由於代碼比較簡單,小編就不再講解,看代碼就明白了,直接把實例帖出來。
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>實時時間</title> <script type="text/javascript"> var slidint; function newTimer() { stopit(); var today; today = new Date(); var str = today.toLocaleDateString(); str += " " + week(); str += " " + today.toLocaleTimeString(); var o = document.getElementById("lblDateTime"); o.innerHTML = str; slidint = setTimeout(newTimer, 1000); } function stopit() { clearTimeout(slidint); } function week() { var d, day, x, s = " "; var x = new Array("星期日", "星期一", "星期二"); var x = x.concat("星期三", "星期四", "星期五"); var x = x.concat("星期六"); d = new Date(); day = d.getDay(); return (s += x[day]); } window.onload = function() { newTimer(); } </script> </head> <body> <label id="lblDateTime"></label> </body> </html>
代碼實現效果如下:
關於js獲取動態時間的方法有很多,需要大家多進行總結,希望本文所述對大家的javascript程序設計有所幫助。