SoFunction
Updated on 2025-02-28

JavaScript implements real-time output of current time

Recently, there is a website page that needs to output the current time, which is accurate to the time, minutes and seconds, and changes in time. I searched for Baidu, but couldn't find a suitable one, so I wrote it myself and collected it for convenience for future use.

js outputs the current time in time

function CurentTime(divID){ 
  var curTime=new Array(); 
  var now=new Date(); 
  var week=['day','one','two','three','Four','five','six']; 
  var year=();   //Year  var month=()+1;   //moon  var day=();      //day  var hh=();      //hour  var mm=();     //point  var sc=();     //Second  var wk=();       //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['sc']=sc<10?'0'+sc:sc; 
  curTime['wk']='Week'+week[wk]; 
  curTime=curTime['year']+'Year'+curTime['month']+'moon'+curTime['day']+'day'+' '+curTime['wk']+' '+curTime['hh']+':'+curTime['mm']+':'+curTime['sc']; 
  (divID).innerHTML='Today is:'+curTime; 
  setTimeout('CurentTime(\''+divID+'\')',1000); 
} 

How to use:

Assuming 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 like it.