Home > Web Front-end > JS Tutorial > js gets the current time and displays it on the page and refreshes it every second_javascript skills

js gets the current time and displays it on the page and refreshes it every second_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 16:24:40
Original
1438 people have browsed it

The method is very simple and the code is also very concise. Just provide the code

Copy code The code is as follows:





js gets the current time and displays it on the page
<script><br> window.onload=function(){<br> //The timer calls fnDate()<br> once every second setInterval(function(){<br> fnDate();<br> },1000);<br> }<br> //js gets the current time <br> function fnDate(){<br> var oDiv=document.getElementById("div1");<br> var date=new Date();<br> var year=date.getFullYear();//Current year<br> var month=date.getMonth();//Current month<br> var data=date.getDate();//day<br> var hours=date.getHours();//Hours<br> var minute=date.getMinutes();//minutes<br> var second=date.getSeconds();//seconds<br> var time=year "-" fnW((month 1)) "-" fnW(data) " " fnW(hours) ":" fnW(minute) ":" fnW(second);<br> oDiv.innerHTML=time;<br> }<br> //Fill in 0 when a field is not a two-digit number <br> function fnW(str){<br> var num;<br> str>10?num=str:num="0" str;<br> return num;<br> } <br> </script>





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
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template