Home >Web Front-end >JS Tutorial >How to obtain JS dynamic date and time_javascript skills
Since the code is relatively simple, the editor will not explain it anymore. You will understand it by looking at the code and post the example directly.
<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>
The code implementation effect is as follows:
There are many ways to obtain dynamic time in js, and everyone needs to summarize them. I hope this article will be helpful to everyone's javascript programming design.