Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
126
rated 0 times [  131] [ 5]  / answers: 1 / hits: 93393  / 9 Years ago, sun, june 28, 2015, 12:00:00

I have a date object that's created by the user, with the timezone filled in by the browser, like so:



var date = new Date(2011, 05, 07, 04, 0, 0);
> Tue Jun 07 2011 04:00:00 GMT+1000 (E. Australia Standard Time)


When I stringify it, though, the timezone goes bye-bye



JSON.stringify(date);
> 2011-06-06T18:00:00.000Z


The best way I can get a ISO8601 string while preserving the browser's timezone is by using moment.js and using moment.format(), but of course that won't work if I'm serializing a whole command via something that uses JSON.stringify internally (in this case, AngularJS)



var command = { time: date, contents: 'foo' };
$http.post('/Notes/Add', command);


For completeness, my domain does need both the local time and the offset.


More From » json

 Answers
19

Assuming you have some kind of object that contains a Date:



var o = { d : new Date() };


You can override the toJSON function of the Date prototype. Here I use moment.js to create a moment object from the date, then use moment's format function without parameters, which emits the ISO8601 extended format including the offset.



Date.prototype.toJSON = function(){ return moment(this).format(); }


Now when you serialize the object, it will use the date format you asked for:



var json = JSON.stringify(o);  //  '{d:2015-06-28T13:51:13-07:00}'


Of course, that will affect all Date objects. If you want to change the behavior of only the specific date object, you can override just that particular object's toJSON function, like this:



o.d.toJSON = function(){ return moment(this).format(); }

[#66013] Friday, June 26, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
margaritakristinak

Total Points: 502
Total Questions: 127
Total Answers: 98

Location: England
Member since Mon, May 17, 2021
3 Years ago
;