javascript date getYear() 在 IE 和 Firefox 之间返回不同的结果,如何解决这个问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5763107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:16:03  来源:igfitidea点击:

javascript date getYear() returns different result between IE and Firefox, how to approach this?

javascriptdatebrowser-detection

提问by Meow

Apparently javascript date object's method getYear() returns different result between IE8 and Firefox3.6 (I have those 2 on my machine, not sure other browser or version)

显然 javascript 日期对象的方法 getYear() 在 IE8 和 Firefox3.6 之间返回不同的结果(我的机器上有这两个,不确定其他浏览器或版本)

Date d = new Date();
alert(d.getYear());

FF3.6 ==> 111 (year since 1900? i guess)
IE8 ===> 2011

I have been only testing on Firefox and now my Javascript code that adjust returned value of getYear() is now giving me 3911 because of my coding.

我一直只在 Firefox 上进行测试,现在由于我的编码,我调整 getYear() 返回值的 Javascript 代码现在给了我 3911。

var modified = d.getYear() + 1900

On Firefox it return 2011. But if I apply this approach on IE8, it return 3911.

在 Firefox 上它返回 2011。但是如果我在 IE8 上应用这种方法,它返回 3911。

I could add logic to distinguish IE and Firefox but I don't want to add such if/else everywhere in my code wherever there are browser dependent parts like this. Is there other way to approach this problem?

我可以添加逻辑来区分 IE 和 Firefox,但我不想在我的代码中随处添加这样的 if/else,只要有这样的浏览器相关部分。有没有其他方法可以解决这个问题?

var browserName=navigator.appName; 

if (browserName=="Netscape") { 
   var modified = d.getYear() + 1900
}
else if(browserName=="Microsoft Internet Explorer") { 
   var modified = d.getYear();
}

回答by psynnott

Use getFullYear()instead of getYear().

使用getFullYear()代替getYear()

回答by Naren Sisodiya

try to use getFullYear() instead getYear

尝试使用 getFullYear() 代替 getYear

回答by T.J. Crowder

If IE8 is giving you 2011, It's a bug in IE8 (and earlier, see update below). getYearis defined in the specification(Section B.2.4) as being:

如果 IE8 为您提供 2011,则这是 IE8 中的错误(以及更早版本,请参阅下面的更新)getYear规范(第 B.2.4 节)中定义为:

  1. Let tbe this time value.
  2. If tis NaN, return NaN.
  3. Return YearFromTime(LocalTime(t)) ? 1900.
  1. t成为这个时间值。
  2. 如果tNaN,则返回NaN
  3. 返回YearFromTime(LocalTime(t)) ? 1900

Thus right now, 111 is the correct value. That definition is unchanged from the 3rd edition, so we're talking ~12 years of specified behavior.

因此,现在 111 是正确的值。该定义与第 3 版相同,因此我们谈论的是约 12 年的特定行为。

As others have said, use getFullYearto get a more useful value, but that's an IE8 bug if it's truly as you say (I don't have IE8 handy to check).

正如其他人所说,使用getFullYear以获得更有用的值,但如果确实如您所说,那是 IE8 错误(我没有 IE8 可以方便地检查)



Update: Well I'll be. Just tried it, and Microsoft didget it wrong. IE6, IE7, and IE8 all say "2011". The good news is they've finally fixed it, IE9 says "111" as it should. You can try it in your browser here: http://jsbin.com/ofuyi3

更新:好吧,我会的。刚刚试了一下,微软确实弄错了。IE6、IE7 和 IE8 都显示“2011”。好消息是他们终于修复了它,IE9 应该说“111”。您可以在浏览器中尝试:http: //jsbin.com/ofuyi3

回答by ikegami

Don't rely on product versions when you don't have to. Instead, rely on the difference you want to correct itself. If you wanted getYear's correct value, you could get it using

不必要时不要依赖产品版本。相反,依靠您想要自行纠正的差异。如果你想要getYear正确的值,你可以使用

Date d = new Date();
var year = d.getYear();
if (year < 1900) {  // Should always be true, but isn't in older IE.
   year += 1900;
}

I realise people have suggested a better way of getting the result, but I thought the actual questionwas worth answering.

我意识到人们已经提出了获得结果的更好方法,但我认为实际问题值得回答。

回答by RobG

Use date.getFullYear();

使用 date.getFullYear();

blah.. gotta answer with at least 30 characters...

等等.. 必须回答至少 30 个字符...