-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
33 lines (28 loc) · 922 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//when num < 10, 0 digit is append before the num digit
function addZero(num){
return num < 10 ? `0${num}` : `${num}`;
}
function currentTime(){
let time = new Date(); // create new object of Date class
let hour = addZero(time.getHours());
let min = addZero(time.getMinutes());
let sec = addZero(time.getSeconds());
var am_pm = "AM";
if(hour == 12) //change to PM when hour = 12
am_pm = "PM";
if (hour > 12){ //change hour to 1 when hour > 12
hour -= 12;
am_pm = "PM";
}
if (hour == 0){ //change to AM and hour to 12 when hour = 0
hour = 12;
am_pm = "AM";
}
//value of current time
let TimeNow = `${hour}:${min}:${sec} ${am_pm}`;
//update html element div id="time" to TimeNow
const clock = document.getElementById("time");
clock.innerHTML = TimeNow;
};
currentTime();
setInterval(currentTime, 1000);