GibboK
GibboK

Reputation: 73938

Microsoft AJAX serialized dates with time swift

I have an MVC 3 web application for a web API, a controller emit json. In the json result I see dates are being serialised automatically

as

{
Flag: "U"
EventId: "168ef1d4-60ca-4fa1-b03b-8c3207650347"
EventTitle: "test event 11"
DateTimeStart: "/Date(1369217469310)/"
IsCustomEvent: true
Location: null
}

in javascript I need to convert DateTimeStart in human readable format and using this code

var date = new Date(1369217469310); alert(date);

I see the resulting data as

Wed May 22 2013 12:11:09 GMT+0200 (CEST)

This is 1 hour a head of the date stored in the application wich is 22/05/2013 11:11:09.

I would like to know where the issue could be and how to fix it:

Please let em know how you would fix it, thanks!

Upvotes: 2

Views: 251

Answers (1)

Slawomir Pasko
Slawomir Pasko

Reputation: 907

I haven't enough information to advice about the server side. Generally the source of the problem on server side may be the CultureInfo set in you application. You may consider to convert all datetimes as UTC before sending it to the browser. Check the DateTime.ToUniversalTime() method.

On client side you also are able to fix the offset between regional time and UTC. There is no build in function to do this, but it's very simple operation to perform. Check the code below.

var date = new Date();
var dateWithOffset = date.getTime() + date.getTimezoneOffset() * 60000;

Upvotes: 1

Related Questions