JavaScript Date.toJSON() produces a date which has wrong hours and minutes

var date = new Date();
console.log(date.toJSON(), new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toJSON());

date.toJSON() prints the UTC-Date into a string formatted as json-date.

If you want your local-time to be printed, you have to use getTimezoneOffset(), which returns the offset in minutes. You have to convert this value into seconds and add this to the timestamp of your date:

var date = new Date(2012, 10, 30, 6, 51);
new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toJSON()

In a previous version of this answer, the offset was erroneously added instead of subtracted.

Tags:

Javascript