Relatively simple JavaScript script with real-time display time revised _ time date

Source: Internet
Author: User
Relatively simple JavaScript script code for real-time display of time. You can see that it has been modified. JavaScript shows the time, and the time is still moving! Not a static effect!
Function Time () defines a function.
{If (! Document. layers &&! Document. all)
Return because IE and Netscape have different interpretations of JavaScript, resulting in different browsing effects, they should write code separately. This statement determines the browser used by the user. If neither of them is returned.
Var timer = new Date () defines a new variable named timer, which is a new Date object.
Var hours = Timer. getHours ()
Var minutes = Timer. getMinutes ()
Var seconds = Timer. getSeconds () defines three variables respectively to obtain the values of the current "Hour", "Minute", and "second.
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, the value is PM and the value is reduced by 12; when the number of hours is less than 12, the value is AM.
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" in front ".
Myclock = "" + hours + ":" + minutes + ":" + seconds + "" + noon +""Combine" hour, minute, second "with a new variable.
If (document. layers)
{Document. layers. position.
Document. write (myclock)
Document.layers.position.doc ument. close ()} If the browser is Netscape, myclock is output and the code used for IE is stopped.
Else if (document. all)
Position. innerHTML = myclock otherwise, if the browser is IE, myclock is output.
SetTimeout ("Time ()", 1000) calls the Time function once every 1000 milliseconds, that is, one second.
When loading an onload = "Time ()" Page, call the Time () function.

<Html> <body> </ptml> <script language = "JavaScript"> <! -- Function Time () {if (! Document. layers &&! Document. all) return; var Timer = new Date () var hours = Timer. getHours () var minutes = Timer. getMinutes () var seconds = Timer. getSeconds () var noon = "AM" if (hours> 12) {noon = "PM" hours = hours-12} if (hours = 0) hours = 12; if (minutes <= 9) minutes = "0" + minutes; if (seconds <= 9) seconds = "0" + seconds; // change font size here to your desire myclock = "" + hours + ":" + minutes + ":" + seconds + "" + noon + "</B> "; if (document. layers) {document.layers.position.doc ument. write (myclock); document.layers.position.doc ument. close ();} else if (document. all) {position. innerHTML = myclock; setTimeout ("Time ()", 1000)} Time (); // --> script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]



Below is the simplest way to display dates

<Script language = Javascript> var now = new Date () document. write ("now" + now. getYear () + "year" + (now. getMonth () + 1) + "month" + now. getDate () + "day" + now. getHours () + "Hour" + now. getMinutes () + "Minute" + now. getSeconds () + "seconds") script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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.