博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

js获取当前时间&js 页面时钟

Posted on 2016-09-06 13:18  来碗酸梅汤  阅读(1319)  评论(0编辑  收藏  举报

js获取当前时间

//获取当前时间,格式YYYY-MM-DD
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }

 

js页面添加时钟

<script type="text/javascript">
function realSysTime(clock){
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth();
    var date = now.getDate();
    var day = now.getDay();
    var hour = now.getHours();
    var minu = now.getMinutes();
    var sec = now.getSeconds();
    month = month+1;
    var arr_week = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
    var week = arr_week[day];
    var time = year+"年"+month+"月"+date+"日"+" "+week+" "+hour+":"+minu+":"+sec;
    clock.innerHTML=time;
}
window.onload=function(){
    window.setInterval("realSysTime(clock)", 1000);
}
</script>

<div id="clock"></div>