问题描述
显然,javascript日期对象的方法getYear()返回IE8和Firefox3.6之间的不同结果(我的机器上有2个,不知道其他浏览器或版本) 日期d = new Date();
alert(d.getYear());
FF3.6 ==> 111(1900年以后我估计)
IE8 ===> 2011
我一直只在Firefox上测试,现在我的Javascript代码调整了返回值的getYear()现在给我3911因为我的编码。
var modified = d.getYear()+ 1900
在Firefox上返回2011年。但如果我在IE8上应用此方法,则返回3911。
我可以添加逻辑来区分IE和Firefox,但是我不想在我的代码中添加任何地方,如果有这样的浏览器依赖的部分。有没有办法解决这个问题?
var browserName = navigator.appName;
if(browserName ==Netscape){
var modified = d.getYear()+ 1900
}
else if(browserName ==Microsoft Internet资源管理器){
var modified = d.getYear();
}
使用.getFullYear()的.getYear()
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)
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.
var modified = d.getYear() + 1900
On Firefox it return 2011. But if I apply this approach on IE8, it return 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?
var browserName=navigator.appName;
if (browserName=="Netscape") {
var modified = d.getYear() + 1900
}
else if(browserName=="Microsoft Internet Explorer") {
var modified = d.getYear();
}
Use .getFullYear() instead of .getYear()
这篇关于javascript日期getYear()在IE和Firefox之间返回不同的结果,如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!