问题描述
为什么这个 javascript 返回 108 而不是 2008?它的日期和月份是正确的,但不是年份?
Why does this javascript return 108 instead of 2008? it gets the day and month correct but not the year?
myDate = new Date();
year = myDate.getYear();
年 = 108?
推荐答案
Y2K,只计算自 1900 年以来的年份.
It's a Y2K thing, only the years since 1900 are counted.
现在存在潜在的兼容性问题,getYear()
已被弃用,取而代之的是 getFullYear()
- 来自 quirksmode:
There are potential compatibility issues now that getYear()
has been deprecated in favour of getFullYear()
- from quirksmode:
为了让事情变得更加复杂,date.getYear() 现在已被弃用,您应该使用 date.getFullYear(),而旧浏览器不支持它.但是,如果它有效,它应该总是给出全年,即.2000 而不是 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:
There are also implementation differences between Internet Explorer and Firefox, as IE's implementation of getYear()
was changed to behave like getFullYear()
- from IBM:
根据 ECMAScript 规范,getYear 返回年份减去 1900,原本打算返回98";1998 年.getYear 在 ECMAScript 版本 3 中被弃用,并替换为 getFullYear().
Internet Explorer 将 getYear() 更改为与 getFullYear() 类似的工作方式并使其符合 Y2k 标准,而 Mozilla 保持标准行为.
Internet Explorer changed getYear() to work like getFullYear() and make it Y2k-compliant, while Mozilla kept the standard behavior.
这篇关于为什么 Javascript getYear() 返回 108?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!