How to receive Date field from json service [duplicate]

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

You need to map the string field to a date one:

getList() : Observable<SomeModel[]> {
  return this._http.get(this._getListUrl).map(data => {
    return this.extractData(data);
  });
}

// parse a date in yyyy-mm-dd format
function parseDate(input) {
  var parts = input.split('-');
  return new Date(parts[0], parts[1]-1, parts[2]); // Note: months are 0-based
}

private extractData(res: Response) {
  var data = res.json().data || [];
  data.forEach((d) => {
    d.dateOfBirth = this.parseDate(d.dateOfBirth);
  });
  return data;
}

See this question: