Javascript outputs the current time in real time
The real-time display time on the webpage not only adds color to the webpage, but also helps the viewer master the current time. In order to improve the website development speed, the main code can be encapsulated in a separate function and called directly when needed. for demonstration purposes, I wrote it on the home page for you to watch.
Recently, the current time must be output on a website page, accurate to minutes and seconds, and must be changed in a timely manner. Baidu did not find a suitable one, so he wrote it and added it to his favorites for later use.
Js outputs current time in due time
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Function CurentTime (divID ){ Var curTime = new Array (); Var now = new Date (); Var week = ['day', 'yi', '2', '3', '4', '5', '6']; Var year = now. getFullYear (); // year Var month = now. getMonth () + 1; // month Var day = now. getDate (); // day Var hh = now. getHours (); // Var mm = now. getMinutes (); // minute Var SC = now. getSeconds (); // second Var wk = now. getDay (); // week CurTime ['Year'] = year; CurTime ['month'] = month <10? '0' + month: month; CurTime ['day'] = day <10? '0' + day: day; CurTime ['hh'] = hh <10? '0' + hh: hh; CurTime ['mm'] = mm <10? '0' + mm: mm; CurTime ['scs'] = SC <10? '0' + SC: SC; CurTime ['wk '] = 'Week' + week [wk]; CurTime = curTime ['Year'] + 'Year' + curTime ['month'] + 'month' + curTime ['day'] + 'day' + ''+ curTime ['wk '] + ''+ curTime ['hh'] + ': '+ curTime ['mm'] +': '+ curTime ['scs']; Document. getElementById (divID). innerHTML = 'Today is: '+ curTime; SetTimeout ('curenttime (\ ''+ divID + '\') ', 1000 ); } |
Usage:
Assume that there is a div whose id is time on the page, then:
?
1 |
<Script language = "javascript"> CurentTime ('time'); </script> |
The above is all the content of this article. I hope you will like it.