为什么Javascript getYear()返回108? [英] Why does Javascript getYear() return 108?

查看:165
本文介绍了为什么Javascript getYear()返回108?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

year = 108?

解决方案

这是一个 Y2K 的事情,只有从1900年开始计算。



现在有一些潜在的兼容性问题, getYear()已被弃用,有利于 getFullYear() - 从 quirksmode


为了使事情更复杂,date.getYear()现在已被弃用,您应该使用date.getFullYear(),而这又不是由旧的浏览器支持。但是,如果它有效,应该总是给全年,即2000,而不是100。



您的浏览器通过以下两种方法提供以下几年:




  *根据getYear()的年份:108 
*根据getFullYear()的年份:2008

Internet Explorer和Firefox之间也有实现差异,因为IE的 getYear()的实现已更改为表现得像 getFullYear() - 从 IBM


根据ECMAScript规范,getYear返回年份减去1900,原来意味着返回98 为1998年。在ECMAScript版本3中不推荐使用getYear,并用getFullYear()替换。



Internet Explorer将getYear()更改为getFullYear(),并使其符合Y2K标准,而Mozilla保持标准行为。



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();

year = 108?

解决方案

It's a Y2K thing, only the years since 1900 are counted.

There are potential compatibility issues now that getYear() has been deprecated in favour of getFullYear() - from 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

There are also implementation differences between Internet Explorer and Firefox, as IE's implementation of getYear() was changed to behave like getFullYear() - from 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 getYear()返回108?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆