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 useclasse
s to represent server responses, preferinterface
s 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 astring
that gets parsed into aDate
bynew
ingDate
. That said, your approach is perfectly solid and correct. – Aluan Haddad Commented Jun 2, 2017 at 6:10
2 Answers
Reset to default 2Need 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.