year-of-linux/countdown.js
2024-08-10 10:54:50 -05:00

30 lines
1.2 KiB
JavaScript

document.addEventListener('DOMContentLoaded', function () {
// Set the date for January 1st of the following year
const targetDate = new Date();
targetDate.setFullYear(targetDate.getFullYear() + 1);
targetDate.setMonth(0);
targetDate.setDate(1);
targetDate.setHours(0, 0, 0, 0);
// Update the countdown every second
setInterval(updateCountdown, 1000);
function updateCountdown() {
const currentDate = new Date();
const timeDifference = targetDate - currentDate;
const days = Math.floor(timeDifference / (1000 * 60 * 60 * 24));
const hours = Math.floor((timeDifference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((timeDifference % (1000 * 60)) / 1000);
document.getElementById('days').textContent = formatTime(days);
document.getElementById('hours').textContent = formatTime(hours);
document.getElementById('minutes').textContent = formatTime(minutes);
document.getElementById('seconds').textContent = formatTime(seconds);
}
function formatTime(time) {
return time < 10 ? `0${time}` : time;
}
});