原文地址:http://blog.sina.com.cn/s/blog_51e457060100h8qr.html 作者:木子狼
最近看了木子狼一下迅雷的招聘,想准备一下也去应聘,然后特意找到曾今在迅雷任职过的小网哥(Rex)咨询了一下迅雷的相关面试情况,据说机试考的JS类的题目不会太难!
比如下面这样一道题目,就是编个当前日期时间出来,先放上代码:
<script type="text/javascript">
function showtime(){
var timenow;
var today=new Date();
var year=today.getYear();
var mon=today.getMonth()+1;
var date=today.getDate();
var day=today.getDay();
switch(day){
}
var hour=today.getHours();
var minu=today.getMinutes();
if(minu<10) minu="0"+minu;
var secd=today.getSeconds();
if(secd<10) secd="0"+secd;
timenow="今天是:"+year+"年"+mon+"月"+date+"日 "+day+" "+hour+":"+minu+":"+secd;
document.getElementByIdx("clock").innerHTML=timenow;
setTimeout("showtime()",1000);
}
window.οnlοad=showtime;
</script>
<div id="clock"></div>
这里又涉及到浏览器兼容问题,这段代码在Microsoft Internet Explorer中执行正常,输出2010年03月16日星期二,但在Mozilla Firefox浏览器中却显示为110年03月16日星期二,这个问题在其它许多网站上都存在,因为浏览器对于JS代码的解释是存在差异性的。
IE中:
today=new Date();
today.getYear()返回的是绝对的公元纪年,2010
Firefox中:
today=new Date();
today.getYear()返回的是相对绝对的年份,年份相对于1900,因为今年是2010,两者相减是110。
这个问题的根源在于2000年问题,以前年份的表示使用2位数字,在ECMAScript Language Specification的规范中是这么描述的Date对象的:
![[转载]Firefox对函数getYear()返回不正确的原因和解决办法 引用内容](https://i-blog.csdnimg.cn/blog_migrate/13472c0e3e273833211014c29169b8c4.gif)
NOTE
The getFullYear method is preferred for nearly all purposes, because it avoids the "year 2000 problem."
When the getYear method is called with no arguments the following steps are taken:
1. Let t be this time value.
2. If t is NaN, return NaN.
3. Return YearFromTime(LocalTime(t)) − 1900.
因此,为了最大范围的兼容性,使用getFullYear() 是正确的,在不同浏览器中都能正确计算。 就解决了获取年份的兼容性问题。