js date setInterval 時間 時鐘 getFullYear ,JavaScript

 

js 建立時鐘:html

1.參考連接:  注意 getYear  (兩位 或者 四位) 改爲 getFullYear

  js操做時間spa

 

 

2.實例:

 

html:code

1 <span class="glyphicon glyphicon-time" style="color:#fff; padding-left: 10px;"></span> <span style="color:#fff;" id="etime"></span>

 

js:htm

 1         function curDateTime(){
 2             var d = new Date();
 3             var year = d.getFullYear();
 4             var month = d.getMonth()+1;
 5             var date = d.getDate();
 6             var day = d.getDay();
 7             var hours = d.getHours();
 8             var minutes = d.getMinutes();
 9             var seconds = d.getSeconds();
10             var ms = d.getMilliseconds();
11             var curDateTime= year;
12             // console.log(year);
13             if(month>9)
14             curDateTime = curDateTime +"-"+month;
15             else
16             curDateTime = curDateTime +"-0"+month;
17             if(date>9)
18             curDateTime = curDateTime +"-"+date;
19             else
20             curDateTime = curDateTime +"-0"+date;
21             if(hours>9)
22             curDateTime = curDateTime +" "+hours;
23             else
24             curDateTime = curDateTime +" 0"+hours;
25             if(minutes>9)
26             curDateTime = curDateTime +":"+minutes;
27             else
28             curDateTime = curDateTime +":0"+minutes;
29             if(seconds>9)
30             curDateTime = curDateTime +":"+seconds;
31             else
32             curDateTime = curDateTime +":0"+seconds;
33             return curDateTime;
34     }
35 
36     function etime()
37     {
38         var etime = curDateTime();
39         $("#etime").html(etime);
40     }

 

頁面加載完成調用:blog

1         $(function() {
2 
3             //顯示時間
4             setInterval('etime()', 1000);
5 
6         });

 

效果:get

 

 

相關文章
相關標籤/搜索