2

I use the method toISOString() to convert a date into a ISO String

 console.log(date);
 var x = date.toISOString();
 console.log(x);

My input date-object contains the following data Wed Apr 01 2020 01:00:00 GMT+0200 (Mitteleuropäische Sommerzeit)

The output of toISOString() is 2020-03-31T23:00:00.000Z

I try to find a solution and explaination about this strange result, because it happens only by the 1st of April - and this is not an april joke. Does somebody can explain the behavior? Or can give me a hint, how I can get the correct value?

michael-mammut
  • 2,595
  • 5
  • 28
  • 46

1 Answers1

9

Simple. Your timezone is +0200 GMT, and toISOString() gives you Zulu time. Everything is correct here.

htshame
  • 6,599
  • 5
  • 36
  • 56
  • 1
    and this problem occures only at the 1st April? – michael-mammut Feb 28 '18 at 14:35
  • @michael-mammut what input did you expect to get? You ask for an iso string, you get the iso string.. no problem. – tehhowch Feb 28 '18 at 14:59
  • if i send `toISOString Wed Apr 01 2020 01:00:00 GMT+0200 (Mitteleuropäische Sommerzeit)`to the `toISOString()` method, the method returns `2020-03-31T23:00:00.000Z` . So why the date has changed, and how can i prevent it? I expacte an ISO String of the 1.April 2020. – michael-mammut Feb 28 '18 at 15:09
  • 2
    _“So why the date has changed”_ - **it hasn’t**. When you subtract 2 hours from 1:00 in the morning, then of course you land at 23:00 _the previous day_ ... What _has_ changed, is that your local time zone is now ahead of UTC by _two_ hours, whereas “yesterday” it was only ahead by one hour. Again: **DST** is the issue here. – CBroe Feb 28 '18 at 15:29