-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
28 lines (23 loc) · 1.01 KB
/
script.js
File metadata and controls
28 lines (23 loc) · 1.01 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
const datetimeInput = document.getElementById('datetime-input');
const startButton = document.getElementById('start-button');
const timerDisplay = document.getElementById('timer');
function startCountdown() {
const selectedDatetime = new Date(datetimeInput.value).getTime();
const countdownInterval = setInterval(() => {
const now = new Date().getTime();
const distance = selectedDatetime - now;
if (distance < 0) {
clearInterval(countdownInterval);
timerDisplay.textContent = 'Countdown Over!';
} else {
const days = Math.floor(distance / (1000 * 60 * 60 * 24));
const hours = Math.floor(
(distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((distance % (1000 * 60)) / 1000);
timerDisplay.textContent = `${days}d ${hours}h ${minutes}m ${seconds}s`;
}
}, 1000);
}
startButton.addEventListener('click', startCountdown);