The javascript page dynamically displays the time varying sample code

  • 2020-03-30 00:57:23
  • OfStack

 
<html> 
<head> 
<title></title> 

<script> 
function getDateDemo(){ 
/* 

//The statement of time
var date = new Date(); 
alert(date);//The current time
alert(date.toLocaleString());//Converts to local time
alert(date.getFullYear());//According to the year
alert(date.getMonth() + 1);//It shows the month 0 to 11, so you have to add 1
alert(date.getDate());//Displays the date in January
alert(date.getDay());//Displays the date of the week and the day of the week
alert(date.getHours());//Acquire hour time
alert(date.getMinutes());//Get the current minute
alert(date.getSeconds());//Gets the current number of seconds
alert(date.getMilliseconds());//Gets the current number of milliseconds
alert(date.getTime());//Gets the value of the milliseconds from midnight on January 1, 1970, to the current time
*/ 
//Obtain year, month, day, hour, minute and second respectively
var myDate = new Date(); 
var year = myDate.getFullYear(); 
var month = myDate.getMonth() + 1; 
var date = myDate.getDate(); 
var hours = myDate.getHours(); 
var minutes = myDate.getMinutes(); 
var seconds = myDate.getSeconds(); 

//Months are shown in double digits such as September
if(month < 10 ){ 
month = "0" + month; 
} 
if(date < 10 ){ 
date = "0" + date; 
} 

//Time together
var dateTime = year + " years " + month + " month " + date + " day " + hours + " when " + minutes + " points " + seconds + " seconds "; 

//document.write(dateTime);// print The current time

var divNode = document.getElementById("time"); 
divNode.innerHTML = dateTime; 

} 
window.setInterval("getDateDemo()",1000);//Call getDateDemo() every 1 second
</script> 
</head> 
<body> 
<div id="time"></div> 
</body> 
</html> 

Related articles: