3

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 : "2016-03-08T17:27:11.9975483+01:00"

JavaScript/angular2 does not recognize this as valid datetime value.

I do have options, but what direction should I go:

  • Server side: Newtonsoft.Json, ...
  • Client side: angular2, ...
  • Others?

Many thankx for your help!

2
  • But if you use new Date("2016-03-08T17:27:11.9975483+01:00"), you get a valid date object in JavaScript with the correct time and date Commented Apr 26, 2016 at 17:55
  • Thankx PierreDuc. See answer for more information. Commented Apr 26, 2016 at 20:23

1 Answer 1

4

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.