gpt4 book ai didi

javascript - new Date() 在 Chrome 或 Firefox 中显示不同的结果

转载 作者:数据小太阳 更新时间:2023-10-29 06:01:14 25 4
gpt4 key购买 nike

奇怪的是,新的 Date() 会在不同的浏览器中产生不同的结果。

在 Chrome 45.0.2454.101 m 中:

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

在 Firefox 40.0.3 中(默认检查器/控制台)

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

附加信息
如果我在 Firefox 中尝试使用 FIREBUG 扩展的控制台,它会像 Chrome 一样运行良好。发生了什么?似乎 Firefox 没有计算偏移量,实际上它比正确日期晚了 2 小时。我在其他工作站上做了测试,似乎都有这个“错误”。

最佳答案

如果您不想包含时区偏移量,您可以使用 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

Firefox 开发控制台的输出:

> 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

但是 00:00:00 GMT+020022:00:00.000Z 只是在 Date 的字符串表示中表示时区偏移的不同方式。区别在于打印到控制台时使用的方法:大多数浏览器使用 .toString() 而 Firefox 使用 .toISOString()。 (已编辑;之前声明 toString 方法实现不同,这是不正确的)。

在 Chrome(Thu Oct 01 2015 00:00:00 GMT+0200)和 Firefox(Date 2015-09-30T22:00:00.000Z)方法中像 .getDate().getMonth() 返回相同的值(分别为 19)。 Date 对象是相同的。

关于javascript - new Date() 在 Chrome 或 Firefox 中显示不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33122006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com