Home > Web Front-end > JS Tutorial > Relatively concise JavaScript script to display time in real time Revised version_time and date

Relatively concise JavaScript script to display time in real time Revised version_time and date

WBOY
Release: 2016-05-16 19:08:03
Original
1093 people have browsed it

JavaScript displays 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 need to write codes separately. This sentence determines the browser used by the user, and if it is neither, it returns.
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() Define 3 variables respectively to get the current "hour", "minute", " seconds" value.
var noon="AM" if (hours>12)
{ noon="PM" hours=hours-12 }
if (hours==0) ​​
hours=12 Define a named For the "noon" variable, when the number of "hours" is greater than 12, its value is PM, and the resulting value is subtracted by 12; when the number of "hours" is less than 12, its 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 "" Use a new variable to change "hours, minutes, seconds" Combined.
if (document.layers)
{ document.layers.position.
document.write(myclock)
document.layers.position.document.close() } If the browser is Netscape, then Output myclock, and the code for IE stops executing.
else if (document.all)
position.innerHTML=myclock Otherwise, if the browser is IE, myclock will be output.
setTimeout("Time()",1000) The Time function is called every 1000 milliseconds, that is, it moves once per second.
onload="Time()" When the page is loaded, the Time() function is called.


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]


The following is The simplest way to display the date

[Ctrl A select all Note:
If you need to introduce external Js, you need to refresh to execute ]
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