Page real time update time JS instance code

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


function startTime() {
                     var today = new Date(); //Define a date object & NBSP;  
                     var yyyy = today.getFullYear(); //Returns the year & NBSP; by the getFullYear() method of the date object;    
                     var MM = today.getMonth() + 1; //Returns the year & PI via the getMonth() method of the date object;    
                     var dd = today.getDate(); //Returns the year & NBSP; through the getDate() method of the date object;      
                     var hh = today.getHours(); //Returns the hour & NBSP; by the getHours method of the date object;  
                     var mm = today.getMinutes(); //Returns the minute & NBSP; by the getMinutes method of the date object;  
                     var ss = today.getSeconds(); //Returns seconds & NBSP; by the getSeconds method of the date object;  
                     //If the value of a minute or hour is less than 10, add 0 to it, such as 15:20:09 & PI if the time is 3:20:9 p.m.  
                     MM = checkTime(MM);
                     dd = checkTime(dd);
                     mm = checkTime(mm);
                     ss = checkTime(ss);
                     var day; //Used to save the week (getDay() method to get the week number)
                     if (today.getDay() == 0) day = " Sunday  "
                     if (today.getDay() == 1) day = " Monday  "
                     if (today.getDay() == 2) day = " Tuesday  "
                     if (today.getDay() == 3) day = " Wednesday  "
                     if (today.getDay() == 4) day = " Thursday  "
                     if (today.getDay() == 5) day = " Friday  "
                     if (today.getDay() == 6) day = " Saturday  "
                     document.getElementById('nowDateTimeSpan').innerHTML = yyyy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss + "   " + day;
                     setTimeout('startTime()', 1000); //Reload the startTime() method every second
                 }
                 function checkTime(i) {
                     if (i < 10) {
                         i = "0" + i;
                     }
                     return i;
                 }

Call: < Strong> Current time :< / strong>
< Font color = "white" > < Span id = "nowDateTimeSpan" > < / span> < / font>            


Related articles: