JSON Stringify changes time of date because of UTC JSON Stringify changes time of date because of UTC json json

JSON Stringify changes time of date because of UTC


Recently I have run into the same issue. And it was resolved using the following code:

x = new Date();let hoursDiff = x.getHours() - x.getTimezoneOffset() / 60;let minutesDiff = (x.getHours() - x.getTimezoneOffset()) % 60;x.setHours(hoursDiff);x.setMinutes(minutesDiff);


JSON uses the Date.prototype.toISOString function which does not represent local time -- it represents time in unmodified UTC -- if you look at your date output you can see you're at UTC+2 hours, which is why the JSON string changes by two hours, but if this allows the same time to be represented correctly across multiple time zones.


Just for the record, remember that the last "Z" in "2009-09-28T08:00:00Z" means that the time is indeed in UTC.

See http://en.wikipedia.org/wiki/ISO_8601 for details.