javascript - 为什么 new Date(1970, 0 ,1).getFullYear() 返回 1969?

标签 javascript

有人可以解释为什么 new Date(1970, 0 ,1).getFullYear() 返回 1969 而不是 1970 吗?

result.textContent = new Date(1970, 0, 1).getFullYear();
<div id=result></div>


归档的 Firefox 错误:https://bugzilla.mozilla.org/show_bug.cgi?id=1093130

最佳答案

看起来很像 Firefox 的 SpiderMonkey 引擎中的时区错误(很可能在它使用的某些库中);在我的实验中,它只影响 *nix 操作系统,不影响 Windows。 (请参阅下文了解时区的原因。)它正在返回

Wed Dec 31 1969 23:00:00 GMT+0000 (BST)

...when of course, we (@wilsonpage, myself, and millions others on UK time) aren't on British Summer Time anymore (and when we are on summer time, it's GMT+0100, not GMT-0100, so it wouldn't make sense for local time to be behind UTC in any case). (Side note: Chrome also shows "BST", but it has the correct date/time.) (Note for Americans: You're used to "xST" meaning "x standard time" [as opposed to "x daylight time"], but here BST stands for "British Summer Time" — e.g., daylight savings time. When we're not on summer time, we're in GMT.)

I see this behavior in Firefox 30 on Linux Mint 16 (apparently the latest in the packages system) and Firefox 33 on that same system (just downloaded and installed from Mozilla directly); I do not see it on Firefox 33 on Windows 8.1.

@wilsonpage has confirmed that he's using OS X and that he's seeing the same time I am (including the "BST" part).

result.textContent = new Date(1970, 0, 1).toString();
<div id=result></div>


时区进入其中(显然会触发错误)的原因是您为该版本的 Date constructor 赋予的值被解释为 UTC,但是 getFullYear返回其在本地时间的值。

关于javascript - 为什么 new Date(1970, 0 ,1).getFullYear() 返回 1969?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26717400/

相关文章:

javascript - 将类添加到返回意外结果的元素

javascript - 在 Vue.js 中覆盖属性和方法的正确方法?

javascript - 现代浏览器中的非阻塞 javascript 和 css。还需要吗?

javascript - HTML 音频 - 我如何检测流是否可用?

javascript - Angularjs groupBy + orderBy

javascript - 如何操作我们在golang中发送和接收的数据

javascript - 从元素中获取 CSS 值

javascript - 替换图表中数据表的标签

javascript - 如何从所选文本中获取相邻字符?

php - jQuery 中的分隔符数字