容器:
<div id="time"></div>
js代码:
window.onload = function(){
showTime();
function checkTime(i){
if(i<10){
i = "0"+i;
}
return i;
}
function showTime(){
var myDate = new Date();
var year = myDate.getFullYear();
var month = myDate.getMonth()+1;
var date = myDate.getDate();
var week = myDate.getDay();
var h = myDate.getHours();
var m = myDate.getMinutes();
var s = myDate.getSeconds();
var arr = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'];
m = checkTime(m);
s = checkTime(s);
document.getElementById('time').innerHTML = year+'年'+month+'月'+date+'日'+arr[week]+'<br/>'+h+'时'+m+'分钟'+s+'秒';
setTimeout(showTime,500);}
}
知识点:
- 各种获取时间的函数;
- 对特殊的值,如月,周,小时,分钟要进行处理;
- setTimeout函数的使用;