今天查看某網站時發現其日期顯示的不對,顯示為:109年12月28日 星期一(我用的是chrome),后來在IE中測試,沒有問題,顯示為:2009年12月28日,最后在FF中測試也是有問題的。我查看其源代碼,如下:
1 <script language="JavaScript" type="text/javascript">
2 var curdate=new Date();
3 day=curdate.getDay();
4 mm=curdate.getMonth()+1;
5 date=curdate.getDate();
6 year=curdate.getYear();
7 var theday;
8 switch(day)
9 {
10 case 1: theday="一"; break;
11 case 2: theday="二"; break;
12 case 3: theday="三"; break;
13 case 4: theday="四"; break;
14 case 5: theday="五"; break;
15 case 6: theday="六"; break;
16 case 0: theday="日"; break;
17 }
18 alert(year);
19 document.write(year+"年"+mm+"月"+date+"日"+" "+"星期"+theday);
20 </script>
看來是第6行的getYear函數出現了問題,我便用getFullYear函數替換它,問題解決了。看來IE與其它瀏覽器的getYear函數實現是不同的,以后做開發時要注意這個特殊問題。