-
Notifications
You must be signed in to change notification settings - Fork 25
/
countdown.js
29 lines (23 loc) · 1.08 KB
/
countdown.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
// Set the event date (YYYY, MM (zero-based), DD, HH, MM, SS)
const eventDate = new Date(2024, 1, 3, 9, 0, 0); // February 3, 2024, 09:00:00
function updateCountdown() {
const now = new Date().getTime();
const distance = eventDate - now;
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);
document.getElementById('days').innerHTML = days;
document.getElementById('hours').innerHTML = hours;
document.getElementById('minutes').innerHTML = minutes;
document.getElementById('seconds').innerHTML = seconds;
if (distance < 0) {
clearInterval(countdown);
document.getElementById('countdown-text').innerHTML = "The event has started!";
document.getElementById('countdown-clock').style.display = 'none';
}
}
// Update the countdown every second
const countdown = setInterval(updateCountdown, 1000);
// Initial call to avoid delay
updateCountdown();