javascript implements real time output of the current time

  • 2020-06-01 08:17:26
  • OfStack

Recently there is a website page to output the current time, the exact time, but also timely changes. Baidu looked for, did not find the right, so he wrote a, collect next, convenient later use.

js outputs the current time in time


function CurentTime(divID){ 
  var curTime=new Array(); 
  var now=new Date(); 
  var week=[' day ','1','2','3','4','5','6']; 
  var year=now.getFullYear();   // years  
  var month=now.getMonth()+1;   // month  
  var day=now.getDate();      // day  
  var hh=now.getHours();      // when  
  var mm=now.getMinutes();     // points  
  var sc=now.getSeconds();     // seconds  
  var wk=now.getDay();       // weeks  
  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 '+week[wk]; 
  curTime=curTime['year']+' years '+curTime['month']+' month '+curTime['day']+' day '+' '+curTime['wk']+' '+curTime['hh']+':'+curTime['mm']+':'+curTime['sc']; 
  document.getElementById(divID).innerHTML=' Today is: '+curTime; 
  setTimeout('CurentTime(\''+divID+'\')',1000); 
} 

Usage:

If there is one div named time on the page, then:


<script language="javascript">CurentTime('time');</script> 

That's all for this article, I hope you enjoy it.


Related articles: