100-project-100-days-website/100_projects/80-simple-countdown-timer/main.js

22 lines
905 B
JavaScript
Raw Normal View History

2023-05-12 13:15:13 +03:00
// 2023-5-13 || 1444-10-22
let countDownDate = new Date("Dec 31, 2025 23:59:59").getTime();
let counter = setInterval(() => {
let dateNow = new Date().getTime();
let dateDiff = countDownDate - dateNow;
let days = Math.floor(dateDiff / (1000 * 60 * 60 * 24));
let hours = Math.floor((dateDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
let minutes = Math.floor((dateDiff % (1000 * 60 * 60)) / (1000 * 60));
let seconds = Math.floor((dateDiff % (1000 * 60)) / 1000);
document.querySelector(".days").innerHTML = days < 10 ? `0${days}` : days;
document.querySelector(".hours").innerHTML = hours < 10 ? `0${hours}` : hours;
document.querySelector(".minutes").innerHTML = minutes < 10 ? `0${minutes}` : minutes;
document.querySelector(".seconds").innerHTML = seconds < 10 ? `0${seconds}` : seconds;
if (dateDiff < 0) {
clearInterval(counter);
}
}, 1000);