The content of this article is about the code implementation of js displaying time on the web page. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <span id="date1"> </span> <script > function $(num1){ return num1<10?"0"+num1:num1; } function setTime(){ var date = new Date(); var year = date.getFullYear(); var month = date.getMonth()+1; var day = date.getDate();//在使用天数的时候使用date来得到 var week = date.getDay();//getday得到的是一周内的第几天 console.log(); var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getSeconds(); var time = year+"-"+$(month)+'-'+$(day)+" "+$(hour)+":"+$(minute)+":"+$(second); document.getElementById("date1").innerHTML=time; } setTime(); setInterval('setTime()',500); </script> </body> </html>
A little trick using javascript is to first get the various times, combine them together through functions, and then use document.getElementById().innerHTML=the combined time. to print.
Note that the function needs to be called once before using setinterval() (to prevent the page from being blank when it is first opened).
Related recommendations:
JavaScript dynamically displays the current date and time on the webpage
js realizes simple display on the webpage Time method_javascript skills
The above is the detailed content of js code implementation to display time on web page. For more information, please follow other related articles on the PHP Chinese website!