New date () shows different results in Chrome or Firefox

The strange thing is, different results in another browser for the new Date ().

In Chrome 45.0.2454.101 m:

new Date(2015,9,1) Thu Oct 01 2015 00:00:00 GMT+0200 (W. Europe Daylight Time) 

In Firefox 40.0.3 (default inspector / console)

 new Date(2015,9,1) Date 2015-09-30T22:00:00.000Z 

Additional Information
If I try in Firefox with the FIREBUG extension console, it works well, like Chrome. What's happening? It seems that Firefox does not consider the offset, in fact it is 2 hours behind the correct date. I did a test on another workstation, and everyone seems to have this "error".

+5
source share
2 answers

This is just the behavior of the debug console. The two date values ​​you provided are the same and are the correct values. You view local time in Chrome, and Firefox selects UTC time in the debug console.

More precisely, Chrome, IE, and most other browsers simply call .toString() on the object, and Firefox calls .toISOString() .

Ff screenshots

Please note that Firefox has an error showing the wrong time zone name (Standard instead of Daylight), but you can see that the debugger value matches the UTC value of ISO8601.

+4
source

IF you do not want the time zone offset to be enabled, you can use Date.UTC

Note. If Date is called as a constructor with more than one argument, the specified arguments represent local time. If UTC is desirable to use a new date (Date.UTC (...)) with the same arguments.

~ MDN

Exiting the Firefox dev console:

 > new Date(2015,9,1) Date 2015-09-30T22:00:00.000Z // reproduces your problem, my local time is GMT+0200 > new Date(Date.UTC(2015,9,1)) Date 2015-10-01T00:00:00.000Z // UTC time 

However, 00:00:00 GMT+0200 and 22:00:00.000Z are just different ways to represent the timeline offset in the Date string view. The difference lies in the method used when printing to the console: most browsers use .toString() , while Firefox uses .toISOString() . (Edited, it was previously indicated that the implementation of the toString method is different from the true one).

The Chrome ( Thu Oct 01 2015 00:00:00 GMT+0200 ) and Firefox ( Date 2015-09-30T22:00:00.000Z ) Date 2015-09-30T22:00:00.000Z , such as .getDate() and .getMonth() , return the same values ​​( 1 and 9 respectively). Date objects are the same.

+4
source

Source: https://habr.com/ru/post/1233690/


All Articles