Skip to content

Commit b08ecfa

Browse files
committed
digital clock
1 parent 88251d1 commit b08ecfa

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

digital-clock/index.html

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Digital Clock</title>
6+
<style>
7+
body {
8+
background-color: #0f172a;
9+
color: #f8fafc;
10+
font-family: system-ui, Arial;
11+
height: 100vh;
12+
display: flex;
13+
justify-content: center;
14+
align-items: center;
15+
flex-direction: column;
16+
}
17+
.clock {
18+
font-size: 70px;
19+
font-weight: bold;
20+
letter-spacing: 2px;
21+
}
22+
.date {
23+
margin-top: 10px;
24+
font-size: 20px;
25+
color: #a5b4fc;
26+
}
27+
</style>
28+
</head>
29+
<body>
30+
<div class="clock" id="clock">00:00:00</div>
31+
<div class="date" id="date">--/--/----</div>
32+
33+
<script>
34+
function updateClock() {
35+
const now = new Date();
36+
let hours = now.getHours();
37+
let minutes = now.getMinutes();
38+
let seconds = now.getSeconds();
39+
console.log(now.getSeconds())
40+
41+
// // Convert to 12-hour format
42+
// const ampm = hours >= 12 ? "PM" : "AM";
43+
// hours = hours % 12 || 12;
44+
45+
// Add leading zeros
46+
hours = String(hours).padStart(2, "0");
47+
minutes = String(minutes).padStart(2, "0");
48+
seconds = String(seconds).padStart(2, "0");
49+
50+
// Update DOM
51+
document.getElementById("clock").textContent = `${hours}:${minutes}:${seconds}`;
52+
document.getElementById("date").textContent = now.toDateString();
53+
}
54+
55+
// Run every second
56+
setInterval(updateClock, 1000);
57+
58+
// Run immediately once to avoid 1s delay
59+
updateClock();
60+
</script>
61+
</body>
62+
</html>

0 commit comments

Comments
 (0)