-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.js
41 lines (35 loc) · 857 Bytes
/
time.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
34
35
36
37
38
39
40
41
// Calling showTime function at every second
setInterval(showTime, 1000);
// Defining showTime funcion
function showTime() {
// Getting current time and date
let time = new Date();
let hour = time.getHours();
let min = time.getMinutes();
let sec = time.getSeconds();
am_pm = "AM";
// Setting time for 12 Hrs format
if (hour >= 12) {
if (hour > 12) hour -= 12;
am_pm = "PM";
} else if (hour == 0) {
hr = 12;
am_pm = "AM";
}
hour =
hour < 10 ? "0" + hour : hour;
min = min < 10 ? "0" + min : min;
sec = sec < 10 ? "0" + sec : sec;
let currentTime =
hour +
":" +
min +
":" +
sec +
am_pm;
// Displaying the time
document.getElementById(
"clock"
).innerHTML = currentTime;
}
showTime();