转载:JS实时显示系统时间

时间:2022-06-15 00:03:22

 (1):首先用JS编写实时显示系统时间的函数 clockon() 只有一个参数bgclock,用于指定显示用于转化后的<div>标记的名称,无返回值,大家进行网站开发时可以将改函数保存在JS文件中,以便重用 代码如下:

 


  <script type="text/javascript">
function clockon(bgclock)
{
    var now = new Date();
    var year = now.getYear();
    var month = now.getMonth();
    var date = now.getDate();
    var day = now.getDay();
    var hour = now.getHours();
    var minu = now.getMinutes();
    var sec = now.getSeconds();
    var week;
    month = month+1;
    if(month<10)month="0"+month;
    if(date<10)date="0"+date;
    if(hour<10)hour="0"+hour;
    if(minu<10)minu="0"+minu;
    if(sec<10)sec="0"+sec;
    var arr_week = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
    week = arr_week[day];
    var time = "";
    time = year+"年"+month+"月"+date+"日"+""+hour+":"+minu+":"+sec+" "+week;
    if(document.all)
    {
        bgclock.innerHTML="["+time+"]"
    }
    var timer = setTimeout("clockon(bgclock)",200);
           
}
</script>

 

html代码:

<body onLoad="clockon(bgclock)">
<div id="bgclock" class="word_Green"></div>

</body>