How to Create Countdown Date in JavaScript

Introduction

In this tutorial we will create a How to Create Countdown Date in JavaScript. This tutorial purpose is to provide you a technique to do the countdown date. This will tackle the applying of a countdown timer for a strong>DateTime value. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you will do it without a problem. This program is useful if you to apply a timer for some event in your system. I will try my best to give you the easiest way of creating this program Countdown Date Timer. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will display the form for the date time entry. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">How to Create Countdown Date</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-6">
  17.                         <div class="form-group">
  18.                                 <h3>Date</h3>
  19.                                 <input type="date" id="date" class="form-control"/>
  20.                         </div>
  21.                         <div class="form-group">
  22.                                 <h3>Time</h3>
  23.                                 <input type="time" id="time" class="form-control"/>
  24.                         </div>
  25.                         <button type="button" class="btn btn-primary" onclick="setCount()">Start Countdown</button>
  26.                 </div>
  27.                 <div class="col-md-6">
  28.                 <center><h2 id="display">00:00:00</h2></center>
  29.                 </div>
  30.                
  31.                
  32.         </div>
  33. </body>
  34. <script src="script.js"></script>
  35. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will start the timer when the inputs is already entered. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function setCount(){
  2.         var date = document.getElementById('date');
  3.         var time = document.getElementById('time');
  4.        
  5.         if(date.value == "" || time.value == ""){
  6.                 alert("Please complete the required field!");
  7.         }else{
  8.                 var countDate = new Date(date.value+" "+time.value).getTime();
  9.        
  10.                 var countDay = new Date(date.value+" "+time.value).getDate();
  11.        
  12.                 var x = setInterval(function(){
  13.                         var nowTime = new Date().getTime();
  14.                         var nowDate = new Date().getDate();
  15.                         var distance = countDate - nowTime;
  16.                         var dayDis = countDay - nowDate;
  17.                        
  18.                         var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  19.                         var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  20.                         var seconds = Math.floor((distance % (1000 * 60)) / 1000);
  21.                        
  22.                         document.getElementById("display").innerHTML = dayDis + "d " + hours + "h "
  23.                         + minutes + "m " + seconds + "s ";
  24.                        
  25.                        
  26.                         if (distance < 0) {
  27.                                 clearInterval(x);
  28.                                 document.getElementById("display").innerHTML = "Time Expired";
  29.                         }
  30.                        
  31.                 }, 1000);
  32.         }
  33. }

In this code we display first the form for our Date Time value. We bind the form field with id so that it will be easier to fetch the date. After that we create a new variable called nowTime and nowDay. We computed the distance by the difference of countDate and nowTime, and also dayDis the value is the difference of countDay and nowDate. After we set our variables we finally calculated the hours, date, and seconds using the Math.floor() function.

Output:

The How to Create Countdown Date in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create Countdown Date in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment