-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.js
39 lines (30 loc) · 931 Bytes
/
clock.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
class DigitalClock {
constructor(element) {
this.element = element;
}
start() {
this.update();
setInterval(() => {
this.update();
}, 500);
}
update() {
const parts = this.getTimeParts();
const minuteFormatted = parts.minute.toString().padStart(2, "0");
const timeFormatted = `${parts.hour}:${minuteFormatted}`;
const amPm = parts.isAm ? "AM" : "PM";
this.element.querySelector(".clock-time").textContent = timeFormatted;
this.element.querySelector(".clock-ampm").textContent = amPm;
}
getTimeParts() {
const now = new Date();
return {
hour: now.getHours() % 12 || 12,
minute: now.getMinutes(),
isAm: now.getHours() < 12
};
}
}
const clockElement = document.querySelector(".clock");
const clockObject = new DigitalClock(clockElement);
clockObject.start();