Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time changes when converting date into ISOString in nodejs

I've got a date string from my database which has this format:

Tue Nov 12 2013 18:14:46 GMT+0100 (CET)

I want to convert it into a ISOString and im currently doing that with:

var iso = new Date(val.text_date).toISOString();

However for some reason the output time is moved 1 hour backwards? This is the output im getting:

2013-11-12T17:14:46.000Z

How can i avoid this?

like image 385
Alosyius Avatar asked May 27 '26 05:05

Alosyius


1 Answers

Short answer: the time is converted into UTC, and your original time was displayed in UTC+1, hence the one hour difference.


The Date.toISOString() method converts the date into a string in the ISO 8601 format. Note that the returned date in your example ends by a Z: 2013-11-12T17:14:46.000Z. As per the Mozilla documentation and Wikipedia:

If the time is in UTC, add a Z directly after the time without a space. Z is the zone designator for the zero UTC offset

like image 133
Paul Mougel Avatar answered May 30 '26 05:05

Paul Mougel