如何在类构造函数中使用日期?

时间:2016-03-12 00:03:23

标签: typescript angular

有json

{"id":1,"name":"123","dateoff":"2016-01-12T13:30:46.358+05:00","available":true}

并有课

export class Compaing {
    constructor(public id: number, public name: string,
                public dateoff: Date, public available:boolean) { }
}

但是当我在angular2中使用时

<Compaing>res.json()

它不起作用。在compaing.dateoff not date中,它是字符串。 如何使用构造函数将json字符串日期解析为Date?

1 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

export class Compaing {
    dateoff:Date;
    constructor(public id: number, public name: string,
                public dateoffstr: string, public available:boolean)        {
       this.dateoff =new Date(dateoffstr);
    }
}