Skip to content

Sagar Kunwar

Menu
  • Home
  • YouTube
  • Blog
  • Contact
  • Privacy Policy
Menu

Create a Simple JavaScript Countdown Timer

Posted on November 25, 2019February 11, 2023 by Sagar Kunwar

In this blog, we are going to learn how we can create a simple countdown timer to embed in any website using JavaScript.

First we need to set the date we’re counting down to,
var countDownDate = new Date(“Jan 1, 2050 15:37:25”).getTime();
Next we need to update the count down for every second so we are going to create a function.
var x = setInterval(function() {

Inside the function we are achieving following tasks.

1. Get today’s date and time
var now = new Date().getTime();
2. And then find the distance between now and the count down date
var distance = countDownDate – now;
3. Calculate days, hours, minutes and seconds from now to distant time
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);


4. Now we can show the Output anywhere in the DOM, here we are appending it to an element with id=”demo”

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


5. Finally if the count down is over, write some text and close the countdown function.

if (distance < 0) {    
clearInterval(x);
document.getElementById(“demo”).innerHTML = “EXPIRED”;}
}, 1000);

6. In your html place the following tag where ever you want to show the timer…

<p id=”demo” style=”text-align: center;  font-size: 60px;  margin-top: 0px;”></p>

Our code looks like this,

<p id=”demo” style=”text-align: center;  font-size: 60px;  margin-top: 0px;”></p>
<script>
  var countDownDate = new Date(“Jan 5, 2050 15:37:25”).getTime();
  var x = setInterval(function() {
  var now = new Date().getTime(); 
  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); 
 
  document.getElementById(“demo”).innerHTML = days + “d ” + hours + “h “
  + minutes + “m ” + seconds + “s “;

  if (distance < 0) {
    clearInterval(x);
    document.getElementById(“demo”).innerHTML = “EXPIRED”;
  }
}, 1000);
</script>

 

Output:

 

 

(Visited 116 times, 1 visits today)

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Understanding the DOM: A JavaScript Hands-On Walkthrough
  • Deploy Your Full-Stack App to Render in Under 10 Minutes
  • Beginner’s Guide: How to Use Firebase Realtime Database
  • Guide to Responsive Images with `srcset`

Categories

  • Blog
  • Javascript
  • PHP
  • Support
  • Uncategorized
  • Web Hosting
November 2019
S M T W T F S
 12
3456789
10111213141516
17181920212223
24252627282930
    Dec »
© 2026 Sagar Kunwar | Powered by Superbs Personal Blog theme