实时显示当前时间,不够10的前导补0

function showTime() {
        var show = document.createElement('div');
        show.setAttribute('id','d1');
        document.body.appendChild(show);
        var d1 = document.getElementById('d1');
        var currentTime = new Date;
        var hours =  currentTime.getHours();
        if(hours < 10) {
            hours = '0' + hours;    
        }
        var minutes = currentTime.getMinutes();
        if(minutes < 10) {
            minutes = '0' + minutes;    
        }
        var seconds = currentTime.getSeconds();
        if(seconds < 10) {
            seconds = '0' + seconds;    
        }
        d1.innerHTML =     currentTime.getFullYear() + '年' + (currentTime.getMonth() + 1)
 + '月' + currentTime.getDate() + '日' + ' ' + hours + '时' + minutes + '分' + seconds;    
     }
    
    window.onload = function() {
        setInterval('showTime()',1000);    
    }
posted @ 2012-09-04 09:53  himi  阅读(457)  评论(0编辑  收藏  举报