Skip to content Skip to sidebar Skip to footer

Dates In .net Vs In Javascript

I have a ASP.NET WEB.API 4 and a Controller returning the following json: { date: '2013-03-14T00:00:00' } I parse it on the client (JavaScript): date = new Date(json.date); //

Solution 1:

In my opinion you will get better result if your controller will return date in ZULU format. So before sending use: ToUniversalTime (http://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime.aspx) Than the result also will be in ZULU format, so you don't have to convert it to LocalTime

Solution 2:

date = new Date(json.date); // json.date being "2013-03-14T00:00:00"

And what timezone is that json.date in? If there's no information about this, the JS Date constructor will assume it is the client's local timezone (and that is quite random). If you want it to be UTC, just add "Z" after your ISO-datestring:

date = newDate(json.date+"Z");

Also always use the [gs]etUTC(FullYear|Month|Date|…) methods then.

Post a Comment for "Dates In .net Vs In Javascript"