代码如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>时钟</title> <script> window.onload = function(){ showTime(); }; function showTime(){ var myDate = new Date(); var year = myDate.getFullYear(); var month = myDate.getMonth() + 1; var date = myDate.getDate(); var weekday = myDate.getDay(); var w = new Array(6); w[0] = "星期日"; w[1] = "星期一"; w[2] = "星期二"; w[3] = "星期三"; w[4] = "星期四"; w[5] = "星期五"; w[6] = "星期六"; var h = myDate.getHours(); var m =myDate.getMinutes(); var s =myDate.getSeconds(); h = checkTime(h); m = checkTime(m); s = checkTime(s); document.getElementById('time').innerHTML = year + '年' + month + '月' + date + '日' + w[weekday] + h + ':' + m + ':' + s; setTimeout(showTime,1000); } function checkTime(i){ if(i < 10){ return '0'+i; }else{ return i; } } </script> </head> <body> <div id="time"></div> </body> </html>
效果如下,时间一直走动: