js实现时钟

<HTML>
<HEAD>
<TITLE>时间日期篇--显示时钟</TITLE>
<meta charset="utf-8">
</HEAD>

<BODY bgcolor="#fef4d2" onload=showclock() >
<br>
<div id="clock"></div>
<script>
function changeClock()
{
    var d = new Date();
    var year=d.getFullYear();
    var month=d.getMonth()+1;
    if(month<10){
        month="0"+month;
    }
    var day=d.getDate();
    if(day<10){
        day="0"+day;
    }
    
    var hours=d.getHours();
    var dn="AM";
    if(hours>12){
        dn="PM";
        hours=hours-12;
    }else if(hours==0){
        hours=12;
    }
    var minute=d.getMinutes();
    if(minute<10){
        minute="0"+minute;
    }
    var seconds=d.getSeconds();
    if(seconds<10){
        seconds="0"+seconds;
    }
     document.getElementById("clock").innerHTML=year+"-"+month+"-"+day+"   "+hours+":"+minute+":"+seconds+"  "+dn;
   // document.getElementById("clock").innerHTML = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
    
}


window.setInterval(changeClock,1000);
</script>
</BODY>

</HTML>

posted @ 2016-05-19 13:51  坚哥威武  阅读(249)  评论(0编辑  收藏  举报