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

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>
原文地址:https://www.cnblogs.com/nshuai/p/5845401.html