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
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 16:24:401439browse

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>





Statement:
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