JavaScript enables real-time output of current time

Source: Internet
Author: User

Displays the time in the Web page in real-time, not only can give the Web page Tim, but also can facilitate the viewer to grasp the current time, in order to improve the speed of web site development, can be encapsulated in a separate function of the main code, in need of direct call and I for the demonstration, directly written in the main page, to facilitate everyone to watch

Recently, there is a Web page to output the current time, accurate to time and minutes, but also timely changes. Baidu looked for, did not find suitable, so they wrote a, collection, easy to use later.

JS timely output The current 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 ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ']; var year=now.getfullyear (); Year var month=now.getmonth () +1; Month var day=now.getdate (); Day var hh=now.gethours (); When Var mm=now.getminutes (); Divided into Var sc=now.getseconds (); Second Var wk=now.getday (); Zhou 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 ']+ ' +curtime[' month ']+ ' month ' +curtime[' Day ']+ ' th ' + ' ' +curtime[' wk ']+ ' ' +curtime[' ' hh ']+ ': ' + curtime[' mm ']+ ': ' +curtime[' SC ']; document.getElementById (divid). Innerhtml= ' Today is: ' +curtime; SetTimeout (' Curenttime (' +divid+ ') ', 1000); }

How to use:

Suppose a div with an ID named time exists on the page:

?

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

The above mentioned is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.