1
+ // const hourEl = document.getElementById("hour");
2
+ // const minuteEl = document.getElementById("minutes");
3
+ // const secondEl = document.getElementById("seconds");
4
+ // const ampmEl = document.getElementById("ampm");
5
+
6
+ // function updateClock() {
7
+ // let h = new Date().getHours();
8
+ // let m = new Date().getMinutes();
9
+ // let s = new Date().getSeconds();
10
+ // let ampm = "AM";
11
+
12
+ // if (h > 12) {
13
+ // h = h - 12;
14
+ // ampm = "PM";
15
+ // }
16
+
17
+ // h = h < 10 ? "0" + h : h;
18
+ // m = m < 10 ? "0" + m : m;
19
+ // s = s < 10 ? "0" + s : s;
20
+
21
+ // hourEl.innerText = h;
22
+ // minuteEl.innerText = m;
23
+ // secondEl.innerText = s;
24
+ // ampmEl.innerText = ampm;
25
+ // setTimeout(() => {
26
+ // updateClock();
27
+ // }, 1000);
28
+ // }
29
+
30
+ // updateClock();
1
31
const hourEl = document . getElementById ( "hour" ) ;
2
32
const minuteEl = document . getElementById ( "minutes" ) ;
3
33
const secondEl = document . getElementById ( "seconds" ) ;
@@ -7,12 +37,10 @@ function updateClock() {
7
37
let h = new Date ( ) . getHours ( ) ;
8
38
let m = new Date ( ) . getMinutes ( ) ;
9
39
let s = new Date ( ) . getSeconds ( ) ;
10
- let ampm = "AM" ;
40
+ let ampm = h >= 12 ? "PM" : "AM" ;
11
41
12
- if ( h > 12 ) {
13
- h = h - 12 ;
14
- ampm = "PM" ;
15
- }
42
+ h = h % 12 ;
43
+ h = h ? h : 12 ; // Convert 0 to 12 for 12-hour format
16
44
17
45
h = h < 10 ? "0" + h : h ;
18
46
m = m < 10 ? "0" + m : m ;
@@ -22,9 +50,8 @@ function updateClock() {
22
50
minuteEl . innerText = m ;
23
51
secondEl . innerText = s ;
24
52
ampmEl . innerText = ampm ;
25
- setTimeout ( ( ) => {
26
- updateClock ( ) ;
27
- } , 1000 ) ;
53
+
54
+ setTimeout ( updateClock , 1000 ) ;
28
55
}
29
56
30
- updateClock ( ) ;
57
+ updateClock ( ) ;
0 commit comments