Home >Web Front-end >JS Tutorial >Javascript realizes real-time output of the current time_javascript skills
Recently, there is a website page that needs to output the current time, which is accurate to hours, minutes and seconds, and needs to change in time. I searched on Baidu but couldn't find a suitable one, so I wrote one myself and saved it for future use.
js outputs the current time in a timely manner
function CurentTime(divID){ var curTime=new Array(); var now=new Date(); var week=['日','一','二','三','四','五','六']; var year=now.getFullYear(); //年 var month=now.getMonth()+1; //月 var day=now.getDate(); //日 var hh=now.getHours(); //时 var mm=now.getMinutes(); //分 var sc=now.getSeconds(); //秒 var wk=now.getDay(); //周 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['sc']=sc<10?'0'+sc:sc; curTime['wk']='星期'+week[wk]; curTime=curTime['year']+'年'+curTime['month']+'月'+curTime['day']+'日'+' '+curTime['wk']+' '+curTime['hh']+':'+curTime['mm']+':'+curTime['sc']; document.getElementById(divID).innerHTML='今天是:'+curTime; setTimeout('CurentTime(\''+divID+'\')',1000); }
How to use:
Assume that there is a div named time on the page, then:
<script language="javascript">CurentTime('time');</script>
The above is the entire content of this article, I hope you all like it.