JavaScript写一个动态的年月日时分秒的时钟方法

作者:日期:2017-11-24 15:10:43 点击:259

 用JavaScript写一个动态的,格式为..年..月..日..时..分..秒的方法。

var timer1 = null;
var oDiv = document.getElementById('div1');
function clock() {
    var time = new Date(),
    y = time.getFullYear(),
    m = time.getMonth() + 1,
    day = time.getDate(),
    week = time.getDay();
    var h = time.getHours(),
    minu = time.getMinutes(),
    s = time.getSeconds(),
    ms = time.getMilliseconds(),
    ts = time.getTime();
    switch (week) {
    case 0:
        week = '日';
        break;
    case 1:
        week = '一';
        break;
    case 2:
        week = '二';
        break;
    case 3:
        week = '三';
        break;
    case 4:
        week = '四';
        break;
    case 5:
        week = '五';
        break;
    case 6:
        week = '六';
        break;
    }
    var str = "今天是" + y + "年" + m + "月" + day + "日 星期" + week + "" + h + "时" + minu + "分" + s + "秒" + ms + "毫秒";
    oDiv.innerHTML = str;

}
clock();
var timer = setInterval(clock, 1000);
oDiv.onclick = function() {
    if (timer) {
        clearInterval(timer);
        timer = null;
    } else {
        clock();
        timer = setInterval(clock, 1000);
    }
}

上一篇: JavaScript中getAttribute()方法

下一篇: javascript培训课程简介--珠峰培训