Skip to content

Instantly share code, notes, and snippets.

@vitaliisili
Created November 20, 2018 19:14
Show Gist options
  • Save vitaliisili/1a8b507be0be6bd8f90eaf841b651c1d to your computer and use it in GitHub Desktop.
Save vitaliisili/1a8b507be0be6bd8f90eaf841b651c1d to your computer and use it in GitHub Desktop.
Timer
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();
startDate = startDate.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 = ("0" + hours).slice(-2);
document.getElementById("minutes").innerHTML = ("0" + minutes).slice(-2);
document.getElementById("seconds").innerHTML = ("0" + seconds).slice(-2);
} else {
return;
}
}
}
(function () {
countdown('07/04/2019 00:00:00 AM');
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment