js 创建时钟:
1.参考链接: 注意 getYear (两位 或者 四位) 改成 getFullYear
2.实例:
html:
1 <span class="glyphicon glyphicon-time" style="color:#fff; padding-left: 10px;"></span> <span style="color:#fff;" id="etime"></span>
js:
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 }
页面加载完成调用:
1 $(function() { 2 3 //显示时间 4 setInterval('etime()', 1000); 5 6 });
效果: