1. 程式人生 > >javascript時間補零

javascript時間補零

javascript時間補零

function add0(Completion) {
    return Completion < 10 ? "0"+Completion:Completion;
    }
    
function systime() {
    var now = new Date();        //獲取Date對象的一個實例
    var hour = now.getHours();    //獲取小時
    var min = now.getMinutes();       //獲取分鐘
    var sec = now.getSeconds();       //獲取秒鐘
    var mouth = now.getMonth() + 1;    //獲取月份
    var year = now.getFullYear();  //獲取年份
    var day = now.getDay();          //獲取星期
    var date = now.getDate();     //獲取日期
    var xingqi = "星期";
    var arr = new Array("日", "一", "二", "三", "四", "五", "六");
    var week = arr[day];
    var time = year + "年" + add0(mouth) + "月" + add0(date) + "日" + "   " + xingqi + week + "   " + add0(hour) + ":" + add0(min) + ":" + add0(sec);

    //alert("當前系統時間:"+time);

    document.getElementById("clock").innerHTML="當前系統時間:"+time;
}
window.onload=function(){
    window.setInterval("systime()",1000);
}

javascript時間補零