JS gets the current time and refreshes it in real time

Time:2021-1-2

design sketch
JS gets the current time and refreshes it in real time

<!DOCTYPE html>

<html>

 <head>

 <meta charset="{CHARSET}" />

 < title > JS gets the current time and refreshes it in real time

 <script>

 //Page load call

 window.onload = function() {

 //Refresh time every 1 second

 setInterval("NowTime()", 1000);

 };

 function NowTime() {

 //Date of acquisition

 var time = new Date();

 var year = time.getFullYear();

 var month = time.getMonth();

 var day = time.getDate();

 //Get time, minutes and seconds

 var h = time.getHours();

 var m = time.getMinutes();

 var s = time.getSeconds();

 //Check if it is less than 10

 h = check(h);

 m = check(m);

 s = check(s);

 document.getElementById("nowtime").innerHTML =

 Current time:+

 year +

 "Year"+

 month +

 "Month"+

 day +

 "Day"+

 h +

 ":" +

 m +

 ":" +

 s;

 }

 //If the time number is less than 10, add a "0" before it.

 function check(i) {

 //Method 1: use ternary operator

 var num;

 i < 10 ? (num = "0" + i) : (num = i);

 return num;

 //Method 2, if statement judgment

 //if(i<10){

 //    i="0"+i;

 //}

 //return i;

 }

 </script>

 </head>

 <body>

 < div id = "nowtime" > display the time here < / div >

 </body>

</html>