Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  




           


JavaScript date parse returns different dates in IE versus Chrome
In Angular JS, dates are automatically serialized from the server in this format (as a string): 2015-01-01T00:00:00

In IE, new Date("2015-01-01T00:00") returns 1/1/15 in local time.
In Chrome, new Date("2015-01-01T00:00") returns 1/1/15 in UTC time, and if you call getDate() or bind it anywhere, you will actually get back the day prior.

Similarly, if you pass a javascript date back to the server, in IE it parses correctly, but in Chrome, the local time offset gets auto-subtracted.

This is obviously an issue.

I assume the serializer/deserializer used by MVC could be overwritten completely, but that could affect all kinds of things in the application, so ensure you are testing all global usage of dates.
Otherwise, a more localized solution is to manually convert javascript dates to/from something the server can handle.

Download: When you fetch a date from a server, it will appear as a string. Call convertFromServerDate to turn it into a valid date that is consistent between IE and Chrome:

this.convertFromServerDate = function (dt) {
if (!dt)
return dt; // undefined
var localDate = new Date(dt);
return new Date(localDate.getUTCFullYear(), localDate.getUTCMonth(), localDate.getUTCDate());
}

Upload: When you pass a date to the server, call convertToServerDate to turn it into a valid string date that the server can parse.

this.convertToServerDate = function (dt) {
if (!dt)
return dt; // undefined

// ensure this is a date in case it was a string
dt = new Date(dt);

return dt.getFullYear() + '-' +
(dt.getMonth() + 1 < 10 ? '0' : '') +
(dt.getMonth() + 1) + '-' +
(dt.getDate() < 10 ? '0' : '') +
dt.getDate();
};

Created By: amos 8/31/2015 4:53:37 PM
Updated: 9/3/2015 2:38:21 PM