js动态适时输出当前的时间(动态显示当前年月日时分秒)

 
更多

js动态适时输出当前的时间(动态显示当前年月日时分秒)。最近有个网站页面上要输出当前时间,精确到时分秒,还要适时变化的。百度找了找,没找到合适的,于是自己写了个,收藏下,方便以后使用。

/**
 * js动态适时输出当前的时间(动态显示当前年月日时分秒)
 * 显示效果如:今天是:2015年03月13日 星期四 15:52:01
 * @param id
 * @constructor
 */
function showCurentTime(id) {
    var curTime = [],
        now = new Date(),
        month = now.getMonth() + 1,
        day = now.getDate(),
        hh = now.getHours(),
        mm = now.getMinutes(),
        sc = now.getSeconds(),
        wk = now.getDay();
    curTime['year'] = now.getFullYear();
    curTime['month'] = month < 10 ? '0' + month : month;
    curTime['day'] = day < 10 ? '0' + day : day;
    curTime['hh'] = hh < 10 ? '0' + hh : hh;
    curTime['mm'] = mm < 10 ? '0' + mm : mm;
    curTime['sc'] = sc < 10 ? '0' + sc : sc;
    curTime['wk'] = '星期' + ['日', '一', '二', '三', '四', '五', '六'][wk];
    curTime = curTime['year'] + '年' + curTime['month'] + '月' + curTime['day'] + '日' + ' ' + curTime['wk'] + ' ' + curTime['hh'] + ':' + curTime['mm'] + ':' + curTime['sc'];
    document.getElementById(id).innerHTML = '今天是:' + curTime;
    setTimeout('showCurentTime(\'' + id + '\')', 1000);
}

使用方法:

//假设页面上存在一个id命名为time的div,则:
<script language="javascript">showCurentTime('time');</script>
打赏

本文固定链接: https://www.cxy163.net/archives/3070 | 绝缘体

该日志由 绝缘体.. 于 2023年06月20日 发表在 未分类 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: js动态适时输出当前的时间(动态显示当前年月日时分秒) | 绝缘体
关键字: , , , ,
【上一篇】
【下一篇】

js动态适时输出当前的时间(动态显示当前年月日时分秒):等您坐沙发呢!

发表评论


快捷键:Ctrl+Enter