-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclock.js
More file actions
82 lines (71 loc) · 1.81 KB
/
clock.js
File metadata and controls
82 lines (71 loc) · 1.81 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const separator = ":";
const clockContainer = document.getElementById("txt");
const dateContainer = document.getElementById("day");
const monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const dayArr = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
function startTime() {
//Function to start displaying current time
//Get the current date and time
const date = new Date();
//Extract hours, minutes, seconds from the current time
let h = date.getHours();
let m = date.getMinutes();
let s = date.getSeconds();
let am_pm = "AM";
//Setting time for 12 hour format
// if(h > 12){
// h = h - 12;
// am_pm = "PM";
// }else if (h==12){
// h = 12;
// am_pm = "AM";
// }
if (h >= 12) {
if (h > 12) h -= 12;
am_pm = "PM";
} else if (h == 0) {
h = 12;
am_pm = "AM";
}
//Call the checkTime() function to add leading zeroes when needed
m = checkTime(m);
s = checkTime(s);
h = checkTime(h);
//This setTimeOut() function is used to call the startTime() function every 1 second (1000 miliseconds)
setTimeout(startTime, 1000);
//Displaying the time using Document Object Model in the browser window
const clockElement = `${h}${separator}${m}${separator}${s} ${am_pm}`;
const dateElement = `${dayArr[date.getDay()]} <br> ${date.getDate()} ${
monthNames[date.getMonth()]
} ${date.getFullYear()}`;
clockContainer.innerHTML = clockElement;
dateContainer.innerHTML = dateElement;
}
//Function to add leading zeroes if the number is less then 10
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}