最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Null date type returns 1970 date value in angular2 - Stack Overflow

programmeradmin1浏览0评论

Getting the data from service and subscribed the data set with interface type in angular2 (typescript).

Interface has date (startDate field name : Date type) field From the service if the startDate field values es null in html page format getting the 01/01/1970 value.

interface :
    export class name { 
     public realStartDate?: Date
}


subscribe : 
this.data = data.map(item => new name (new Date(item.startDate))

Getting the data from service and subscribed the data set with interface type in angular2 (typescript).

Interface has date (startDate field name : Date type) field From the service if the startDate field values es null in html page format getting the 01/01/1970 value.

interface :
    export class name { 
     public realStartDate?: Date
}


subscribe : 
this.data = data.map(item => new name (new Date(item.startDate))
Share Improve this question edited Jun 2, 2017 at 5:55 Aluan Haddad 31.9k10 gold badges83 silver badges95 bronze badges asked Jun 2, 2017 at 5:51 Govinda rajGovinda raj 6635 gold badges16 silver badges31 bronze badges 5
  • 3 What are you expecting? – Saravana Commented Jun 2, 2017 at 5:55
  • item.startDate is possibly null. Put a debugger and check. new Date(null) return 01/01/1970 – adiga Commented Jun 2, 2017 at 5:56
  • If you want to default to current date use item.startDate ? new Date(items.startDate) : new Date(). Also, your class is a bad idea. never use classes to represent server responses, prefer interfaces for that. In this case, you don't need a wrapper object though. – Aluan Haddad Commented Jun 2, 2017 at 5:57
  • 1 @AluanHaddad—or new Date(item.startDate || Date.now()). ;-) – RobG Commented Jun 2, 2017 at 6:00
  • @RobG yeah that is terser, but slightly more plex since it involves calling Date.now to produce a string that gets parsed into a Date by newing Date. That said, your approach is perfectly solid and correct. – Aluan Haddad Commented Jun 2, 2017 at 6:10
Add a ment  | 

2 Answers 2

Reset to default 2

Need to add a null check for item.startDate.

"new Date(item.startDate)" returns 01/01/1970 when value of item.startDate is null.

Try to write it as follows:

subscribe : 
this.data = data.map(item => new name (item.startDate==null?new Date():new Date(item.startDate))

If you want to default to current date use item.startDate ? new Date(items.startDate) : new Date(). Also, your class is a bad idea. Do not use classes to represent such objects, use interfaces instead.

Regardless, what you need is essentially

dated.ts

export interface Dated { 
  realStartDate?: Date;
}

service-items-service.ts

import {Inject} from '@angular/core';
import {Http} from '@angular/http';
import {Observable} from 'rxjs';
import 'rxjs/add/operator/map';

import {Dated} from './dated';

@Inject export default class DatedItemsService {
  constructor(readonly http: Http) {}      

  get(id): Observable<Dated> {
    return this.http.get(`/api/v1/dateditems/${id}`)
      .map(response => response.json())
      .map(({startDate}) => ({
        realStartDate: startDate ? new Date(startDate): undefined
      });
  }
}

EDIT: Based on ments, it appears an empty value is desired in place of the current date, so I have updated the answer accordingly.

发布评论

评论列表(0)

  1. 暂无评论