js實現年月日星期時分秒時鐘

<div id="timeDiv"></div>

var timeDiv = document.getElementById('timeDiv');

function addZero(value){    
    return value < 10 ? "0" + value : value;
}

function getTime(){    
    var time = new Date();    

    var year = time.getFullYear();    
    var month = time.getMonth() + 1;    
    var day = time.getDate();    
    var hour = time.getHours();    
    var minus = time.getMinutes();   
    var second = time.getSeconds(); 

    var week = time.getDay();    
    var area = '日一二三四五'   

    timeDiv.innerHTML = year +"年"+addZero(month)+"月"+addZero(day)+"日 星期"+area[week]+addZero(hour)+"時"+addZero(minus)+"分"+addZero(second)+"秒"
}

getTime()
window.setInterval(getTime, 1000)
相關文章
相關標籤/搜索