Convert string to date using moment.js

Deniz Adıbelli picture Deniz Adıbelli · Jul 11, 2017 · Viewed 15k times · Source

This my model.ts

 export class Feature2 {

 requestRouteTemplate: string;
 requestMethod: string;
 numberCount: number;
 requestDate: date;

 constructor(values: Object = {}) {
  Object.assign(this, values);  
 }
}

This is component.ts

   this.datas2 = [
  {
    'requestRouteTemplate': 'api/Tasks',
    'requestMethod': 'POST',
    'numberCount': 6,
    'requestDate': '07/01/2017',
  },
  {
    'requestRouteTemplate': 'api/Tasks',
    'requestMethod': 'POST',
    'numberCount': 3,
    'requestDate': '07/02/2017',
  },

I want to convert the request date variable from string to date using the moment library.

Answer

DDeis picture DDeis · Jul 11, 2017

In your constructor add: this.requestDate = moment(values.requestDate, "MM-DD-YYYY").toDate();

See the documentation: