gpt4 book ai didi

javascript - 为什么 Javascript getYear() 返回一个三位数?

转载 作者:IT王子 更新时间:2023-10-29 02:44:06 26 4
gpt4 key购买 nike

为什么这个 javascript 返回 108 而不是 2008?它得到正确的日期和月份,但不是年份?

myDate = new Date();
year = myDate.getYear();

年 = 108?

最佳答案

这是一个Y2K事情,只计算自 1900 年以来的年份。

getYear() 已被弃用,取而代之的是 getFullYear(),存在潜在的兼容性问题 - 来自 quirksmode :

To make the matter even more complex, date.getYear() is deprecated nowadays and you should use date.getFullYear(), which, in turn, is not supported by the older browsers. If it works, however, it should always give the full year, ie. 2000 instead of 100.

Your browser gives the following years with these two methods:

* The year according to getYear(): 108
* The year according to getFullYear(): 2008

Internet Explorer 和 Firefox 之间也存在实现差异,因为 IE 的 getYear() 实现被更改为表现得像 getFullYear() - 来自 IBM :

Per the ECMAScript specification, getYear returns the year minus 1900, originally meant to return "98" for 1998. getYear was deprecated in ECMAScript Version 3 and replaced with getFullYear().

Internet Explorer changed getYear() to work like getFullYear() and make it Y2k-compliant, while Mozilla kept the standard behavior.

关于javascript - 为什么 Javascript getYear() 返回一个三位数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/98124/

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