js gets the current time displayed on the page and refreshed every second

  • 2020-05-09 18:08:05
  • OfStack

The method is very simple, the code is very simple, just give me the code


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js Gets the current time to display on the page </title>
<script>
window.onload=function(){
// Timer calls per second 1 time fnDate()
setInterval(function(){
fnDate();
},1000);
}
//js Get the current time
function fnDate(){
var oDiv=document.getElementById("div1");
var date=new Date();
var year=date.getFullYear();// The current year
var month=date.getMonth();// The current month
var data=date.getDate();// day
var hours=date.getHours();// hours
var minute=date.getMinutes();// points
var second=date.getSeconds();// seconds
var time=year+"-"+fnW((month+1))+"-"+fnW(data)+" "+fnW(hours)+":"+fnW(minute)+":"+fnW(second);
oDiv.innerHTML=time;
}
// Fill a When a field is not a two-digit number 0
function fnW(str){
var num;
str>10?num=str:num="0"+str;
return num;
}
</script>
</head>
<body>
<div id="div1"> </div>
</body>
</html>


Related articles: