JavaScript display time

Source: Internet
Author: User

JavaScript shows time, time is still moving! Not a static effect!

Demo Address: http://www.ijavascript.cn/tools/jsdemo/320/time.htm

Functions time () defines a function. Www.120hrb.com

{if (!document.layers&&!document.all)

return because the IE and Netscape to the JavaScript explanation different, causes the browsing effect to be different, therefore must write the code separately. This sentence determines the browser used by the user, if neither, returns.

var timer=new date () defines a new variable, named Timer, for a new Date object.

var hours=timer.gethours ()

var minutes=timer.getminutes ()

var seconds=timer.getseconds () Defines 3 variables to get the current "hour", "Minute", "second" value.

var noon= "AM" if (hours>12)

{noon= "PM" hours=hours-12}

if (hours==0)

HOURS=12 defines a variable named "noon" when the number of hours is greater than 12 o'clock, its value is PM, the value is reduced by 12, and the value is am when the number of hours is less than 12 o'clock.

if (minutes<=9)

minutes= "0" +minutes

if (seconds<=9)

seconds= "0" +seconds if the number of minutes or seconds is less than 9, add a "0" to the front.

myclock= "" +hours+ ":" +minutes+ ":" +seconds+ "" +noon+ "with a new variable to" hours, minutes, seconds "together.

if (document.layers)

{document.layers.position.

document.write (Myclock)

Document.layers.position.document.close ()} If the browser is Netscape, the output is Myclock, and the code for IE stops executing.

else if (document.all) www.hrbfkyy120.com

Position.innerhtml=myclock Otherwise, the browser is IE, on the output myclock.

The SetTimeout ("Time ()", 1000) every 1000 milliseconds, invokes the function of the moment, that is, one second.

When the "Time ()" page is loaded, the onload= () function is called.

The following is the simplest way to display a date

JavaScript display time

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.