javascript实现数字时钟

HTML Code:

<div id="clock"></div>

JS Code:

function changeClock() {
    var d = new Date();
    var mm = (d.getMinutes() < 10) ? ('0' + d.getMinutes()):d.getMinutes();  //条件运算符给小于10的时间前补0
    var ss = (d.getSeconds() < 10) ? ('0' + d.getSeconds()):d.getSeconds();
    document.getElementById("clock").innerHTML = d.getHours() + ":" + mm + ":" + ss;
}
window.setInterval(changeClock, 1000);

 

 

 

posted @ 2014-03-18 15:51  屋大明  阅读(290)  评论(0编辑  收藏  举报