Home > Web Front-end > JS Tutorial > body text

Javascript realizes real-time output of the current time_javascript skills

WBOY
Release: 2016-05-16 16:02:03
Original
1334 people have browsed it

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&#63;'0'+month:month; 
  curTime['day']=day<10&#63;'0'+day:day; 
  curTime['hh']=hh<10&#63;'0'+hh:hh; 
  curTime['mm']=mm<10&#63;'0'+mm:mm; 
  curTime['sc']=sc<10&#63;'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); 
} 
Copy after login

How to use:

Assume that there is a div named time on the page, then:

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

The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!