PDA

View Full Version : JSON serialization of Date should be in UTC



mikaelstaldal
25 Jan 2011, 2:54 AM
Ext JS 3.3.1 serialize Date to JSON with local time zone. This is wrong. Browsers with native JSON support, such as Firefox, uses UTC. So does the open source reference implementation of JSON.stringify and Date.toJSON:
https://github.com/douglascrockford/JSON-js/blob/master/json.js

It should serialize Date in UTC with a trailing 'Z'.

The solution is to redefine Ext.util.JSON.encodeDate like this (code from the link above):


Ext.util.JSON.encodeDate = function(d) {
function f(n) {
return n < 10 ? '0' + n : n;
}
return '"'+
d.getUTCFullYear() + '-' +
f(d.getUTCMonth() + 1) + '-' +
f(d.getUTCDate()) + 'T' +
f(d.getUTCHours()) + ':' +
f(d.getUTCMinutes()) + ':' +
f(d.getUTCSeconds()) + 'Z' +
'"';
};