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

作者:日期:2014-02-27 18:03:12 点击:152

用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如何实现合并数组中相同的项

下一篇: JavaScript中getAttribute()方法