Open In App

Create a Countdown using Bootstrap 5

Last Updated : 13 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The countdown timer displays the remaining time from a set future date, showing days, hours, minutes, and seconds until the specified event or deadline. In this article, we will create a countdown timer using Bootstrap.

Preview

gfg

Prerequisites

Approach

The provided HTML, CSS, and JavaScript create a countdown timer. Initially hidden, the timer box contains an input field for setting the countdown date and a button. Upon button click, the timer box becomes visible, triggering JavaScript to start a timer. The timer calculates the difference between the set date and the current time, updating the displayed countdown every second. When the countdown reaches zero, it displays an “EXPIRED” message. The code utilizes Bootstrap for styling and ensures a user-friendly countdown timer experience.

Example: This example shows the implementation of the above-explained approach.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Countdown Timer</title>
  <link href=
        rel="stylesheet">
  <style>
    .countdown {
      display: none; /* Initially hide */
      justify-content: center;
      align-items: center;
      font-size: 3rem;
    }
    .container {
      padding-top: 50px;
    }
    .timer-box {
      border: 1px solid #ccc;
      border-radius: 10px;
      padding: 20px;
    }
  </style>
</head>
<body>
   
  <div class="container">
    <div class="row">
      <div class="col-md-6 offset-md-3">
        <h1 class="text-center mb-4">Countdown Timer</h1>
      </div>
    </div>
    <div class="row">
      <div class="col-md-6 offset-md-3">
        <div class="timer-box">
          <div class="form-group">
            <label for="countdownDate">Set Countdown Date and Time:</label>
            <input type="datetime-local" class="form-control" id="countdownDate">
          </div>
          <button class="btn btn-primary mt-2 w-100" onclick="startCountdown()">
            Start Countdown
          </button>
          <div class="countdown mt-4" id="countdown">
            <span id="days">00</span> :
            <span id="hours">00</span> :
            <span id="minutes">00</span> :
            <span id="seconds">00</span>
          </div>
        </div>
      </div>
    </div>
  </div>
 
  <script>
    let countdownDate;
 
    function startCountdown() {
      // Get the date and time set by the user
      countdownDate = new Date(document.getElementById("countdownDate")
                               .value).getTime();
 
      // Show the countdown clock
      document.getElementById("countdown").style.display = "flex";
 
      // Update the countdown every 1 second
      let x = setInterval(function() {
        // Get the current date and time
        let now = new Date().getTime();
         
        // Calculate the distance between now and the countdown date
        let distance = countdownDate - now;
         
        // Calculate days, hours, minutes and seconds
        let days = Math.floor(distance / (1000 * 60 * 60 * 24));
        let hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        let minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        let seconds = Math.floor((distance % (1000 * 60)) / 1000);
         
        // Display the result
        document.getElementById("days").innerHTML = days.
            toString().padStart(2, '0');
        document.getElementById("hours").innerHTML = hours.
            toString().padStart(2, '0');
        document.getElementById("minutes").innerHTML = minutes.
            toString().padStart(2, '0');
        document.getElementById("seconds").innerHTML = seconds.
            toString().padStart(2, '0');
         
        // If the countdown is over, display a message
        if (distance < 0) {
          clearInterval(x);
          document.getElementById("countdown").innerHTML = "EXPIRED";
        }
      }, 1000);
    }
  </script>
 
</body>
</html>


Output:

gfg



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads