function parseJsonDate(jsonDate) { var offset = new Date().getTimezoneOffset() * 60000; var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate); if (parts[2] == undefined) parts[2] = 0; if (parts[3] == undefined) parts[3] = 0; return new Date(+parts[1] + offset + parts[2] * 3600000 + parts[3] * 60000); };
Tuesday, May 28, 2013
C# DateTime to Javascript issues with timezone
I ran into an interesting problem when serializing a C# DateTime object as JSON and returning it via a controller to javascript. The timezone mechanism was causing my times to shift to a localized time based on the originating timezone offset to the local timezone of the web host. Normally, that might not be a huge issue, but since my application was displaying appointments which were being pulled from a localized data store to a user in that same region, the server timezone is irrelevant and the time for the appointment has to be the same time regardless of the timezone. After much digging around, I found this little snippet that seems to answer the problem.
Labels:
.NET,
C#,
Javascript
Subscribe to:
Posts (Atom)