Skip to content Skip to sidebar Skip to footer

ASP.NET WebApi DateTimeOffset Serialize To Json/JavaScript (angular2)

I do not find a nice way to get a DateTimeOffset value to JavaScript (angular2). I am using WebApi (5.2.3) and angular2. On the wire I see the date as follow: RecordModifiedAt :

Solution 1:

Thankx to PierreDuc feedback I have played around and I came to the following conclusion:

Since JSON does not support a Date datatype, I assume one has to make the conversion on the client side. I use the following 'pattern' (see http://codegur.com/36681078/angular-2-date-deserialization):

getTags() {
    return this.http.get('/api/tag/getAll')
        .map((response: Response) => this.convertData(response));
}

private convertData(response: Response) {
    var data = response.json() || [];
    data.forEach((d) => {
        // Convert to a Date datatype
        d.RecordModifiedAt = new Date(d.RecordModifiedAt);
    });
    return data;
}

Post a Comment for "ASP.NET WebApi DateTimeOffset Serialize To Json/JavaScript (angular2)"