More concise JavaScript real-time display time script correction version _ time and date

Source: Internet
Author: User
Tags time and date
JavaScript shows time, time is still moving! Not a static effect!
Functions time () defines a function.
{if (!document.layers&&!document.all)
Return because IE differs from Netscape's interpretation of JavaScript, which results in different browsing effects, write code separately. This sentence to determine the user's browser, if both are not, return.
The 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 obtain 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, the value is PM, and the resulting value is reduced by 12; When the number of hours is less than 12 o'clock, 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" to the front.
myclock= "<font color=blue>" +hours+ ":" +minutes+ ":" +seconds+ "" +noon+ "</b></font>" with a new variable to put "hours, minutes, seconds "Combined.
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)
Position.innerhtml=myclock Otherwise, the browser is IE, the output myclock.
SetTimeout ("Time ()", 1000) every 1000 milliseconds, call a moment function, that is, one second move.
The time () function is called when the onload= page is loaded.
<textarea id="runcode27505"><ptml> <body> <span id= "position" ></span> </body> </ptml> <script Langua Ge= "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= "<font size= ' 4 ' face= ' Arial ' color=blue>" +hours+ ":" +minutes+ ":" +sec onds+ "" +noon+ "</b></font>"; if (document.layers) {document.layers.position.document.write (myclock); Document.layers.position.document.close (); else if (document.all) {position.innerhtml=myclock; SetTimeout ("Time ()", 1000)}} time (); --> </script></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


This is the simplest way to display a date
<script language=javascript> var now=new Date () document.write ("Now" +now.getyear () + "year" + (Now.getmonth () +1) + "Month" +now.getdate () + "Day" +now.gethours () + "Time" +now.getminutes () + "Min" +now.getseconds () + "SEC") </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.