Skip to content

Commit b16df21

Browse files
committed
Fixed sahandghavidel#9 ampm is not working
1 parent b5e86c9 commit b16df21

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

projects/digital-clock/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
<meta charset="UTF-8" />
55
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<title>Digital Clock</title>
7+
<title>Rolex Clock</title>
88
<link rel="stylesheet" href="style.css" />
99
</head>
1010
<body>
11-
<h2>Digital Clock</h2>
11+
<h2 style="text-align: center;">Digital Clock</h2>
1212
<div class="clock">
1313
<div>
1414
<span id="hour">00</span>

projects/digital-clock/index.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
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();
131
const hourEl = document.getElementById("hour");
232
const minuteEl = document.getElementById("minutes");
333
const secondEl = document.getElementById("seconds");
@@ -7,12 +37,10 @@ function updateClock() {
737
let h = new Date().getHours();
838
let m = new Date().getMinutes();
939
let s = new Date().getSeconds();
10-
let ampm = "AM";
40+
let ampm = h >= 12 ? "PM" : "AM";
1141

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
1644

1745
h = h < 10 ? "0" + h : h;
1846
m = m < 10 ? "0" + m : m;
@@ -22,9 +50,8 @@ function updateClock() {
2250
minuteEl.innerText = m;
2351
secondEl.innerText = s;
2452
ampmEl.innerText = ampm;
25-
setTimeout(() => {
26-
updateClock();
27-
}, 1000);
53+
54+
setTimeout(updateClock, 1000);
2855
}
2956

30-
updateClock();
57+
updateClock();

projects/digital-clock/style.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
body {
2-
margin: 0;
2+
/* margin: 0; */
33
font-family: sans-serif;
44
display: flex;
55
flex-direction: column;
@@ -8,7 +8,7 @@ body {
88
justify-content: center;
99
background: url("https://images.unsplash.com/photo-1499002238440-d264edd596ec?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1470&q=80");
1010
background-size: cover;
11-
overflow: hidden;
11+
/* overflow: hidden; */
1212
}
1313

1414
h2 {

0 commit comments

Comments
 (0)