Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Countdown timer in pure JavaScript
function countdown(endDate) {
let days, hours, minutes, seconds;
endDate = new Date(endDate).getTime();
if (isNaN(endDate)) {
return;
}
setInterval(calculate, 1000);
function calculate() {
let startDate = new Date().getTime();
let timeRemaining = parseInt((endDate - startDate) / 1000);
if (timeRemaining >= 0) {
days = parseInt(timeRemaining / 86400);
timeRemaining = (timeRemaining % 86400);
hours = parseInt(timeRemaining / 3600);
timeRemaining = (timeRemaining % 3600);
minutes = parseInt(timeRemaining / 60);
timeRemaining = (timeRemaining % 60);
seconds = parseInt(timeRemaining);
document.getElementById("days").innerHTML = parseInt(days, 10);
document.getElementById("hours").innerHTML = hours < 10 ? "0" + hours : hours;
document.getElementById("minutes").innerHTML = minutes < 10 ? "0" + minutes : minutes;
document.getElementById("seconds").innerHTML = seconds < 10 ? "0" + seconds : seconds;
} else {
return;
}
}
}
(function () {
countdown('04/01/2333 05:00:00 PM');
}());
@oyenmwen

This comment has been minimized.

Copy link

@oyenmwen oyenmwen commented Nov 19, 2019

Thank you so much for this!!! Everything else is too overly complicated :)

@adriennetacke

This comment has been minimized.

Copy link
Owner Author

@adriennetacke adriennetacke commented Nov 19, 2019

@oyenmwen, glad it helps! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment