最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - new Date() shows differents results in Chrome or Firefox - Stack Overflow

programmeradmin0浏览0评论

Strange thing, different results in differente browser for a 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 info
If I try in Firefox with the console of FIREBUG extension, works well like Chrome. What's happening? Seems that Firefox doen'st count the offset, in fact it's 2 hour behind the correct date. I did the test on other workstation and all seems to have this "bug".

Strange thing, different results in differente browser for a 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 info
If I try in Firefox with the console of FIREBUG extension, works well like Chrome. What's happening? Seems that Firefox doen'st count the offset, in fact it's 2 hour behind the correct date. I did the test on other workstation and all seems to have this "bug".

Share Improve this question asked Oct 14, 2015 at 9:47 KrekerKreker 2,4883 gold badges23 silver badges27 bronze badges 1
  • 2 Could this be related to stackoverflow./a/15110385/5147646? – Alex Booker Commented Oct 14, 2015 at 9:53
Add a ment  | 

2 Answers 2

Reset to default 5

IF you don't want the timezone offset to be included you can use Date.UTC

Note: Where Date is called as a constructor with more than one argument, the specifed arguments represent local time. If UTC is desired, use new Date(Date.UTC(...)) with the same arguments.

~MDN

Output from 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 timezone offset in Date's string representation. The difference is the method used when printing to console: most browsers use .toString() while Firefox uses .toISOString(). (Edited; previously stated that the toString method implementations are different which isn't true).

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

This is just the behavior of the debug console. The two date values you showed are both the same, and are the correct value. You're just seeing the local time in Chrome, while Firefox chooses to show the UTC time in the debug console.

More accurately, Chrome, IE, and most other browsers simply call .toString() on the object, while Firefox is calling .toISOString().

Note that Firefox has a bug that us showing the wrong name of the time zone (Standard instead of Daylight), but you can see the debugger value matches the ISO8601 UTC value.

发布评论

评论列表(0)

  1. 暂无评论