Countdown timer using Javascript

Here is the full javascript tutorial for creating a countdown timer to a certain date. This count down timer contains days, hours, minutes and seconds as well.


// Set the date to which the timer continues

var countDownDate = new Date("Aug 1, 2017 12:00:00").getTime();

var x = setInterval(function() {

// Todays date and time

var now = new Date().getTime();

// Distance between now and the countdown date

var distance = countDownDate - now;

var days = Math.floor(distance / (1000 * 60 * 60 * 24));

var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));

var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));

var seconds = Math.floor((distance % (1000 * 60)) / 1000);

// Putting the countdown timer in element with ID demo

document.getElementById("demo").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";

// If countdown is over, then putting the text in element with ID demo

if (distance < 0) {

clearInterval(x);

document.getElementById("demo").innerHTML = "EXPIRED";

}

}, 1000);

One more thing to do, just place the below code in your body :


<p id="demo"></p>