Countdown Timer: Building with JavaScript and CSS

Countdown timers are useful tools for creating a sense of urgency or providing a visual representation of time remaining for various purposes, such as event launches, sales promotions, or online quizzes. In this tutorial, we will walk you through the process of creating a countdown timer using JavaScript and CSS. By following these step-by-step instructions, you’ll be able to add an interactive countdown timer to your website or web application effortlessly.

Github Repo is here!

Setting Up the HTML Structure

To start, we need to set up the basic HTML structure for our countdown timer. Open your preferred text editor and create a new HTML file. Begin by creating a container element where our countdown timer will reside. Give it a unique ID to reference it in our JavaScript later. Here’s an example code snippet:

<!DOCTYPE html>
<html>
<head>
  <title>Countdown Timer</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div id="countdown-container">
    <div id="days">0</div>
    <div id="hours">0</div>
    <div id="minutes">0</div>
    <div id="seconds">0</div>
  </div>

  <script src="script.js"></script>
</body>
</html>

Styling the Countdown Timer with CSS

Now, let’s make our countdown timer visually appealing using CSS. Create a new file named “styles.css” and link it to the HTML file. Apply styles to the countdown container and the individual timer elements. Here’s an example CSS code snippet:

#countdown-container {
  display: flex;
  justify-content: center;
  align-items: center;
}

#countdown-container div {
  margin: 0 10px;
  font-size: 2rem;
  color: #fff;
  background-color: #333;
  padding: 10px 20px;
  border-radius: 5px;
}

Implementing the JavaScript Logic

To make our countdown timer functional, we’ll use JavaScript to calculate and update the countdown time remaining. Create a new file named “script.js” and link it to the HTML file. We’ll start by defining some variables and a function to initialize the countdown. Here’s an example JavaScript code snippet to get you started:

// Get the countdown container element
const countdownContainer = document.getElementById("countdown-container");

// Set the target date and time (adjust it according to your needs)
const targetDate = new Date("2023-12-31T23:59:59").getTime();

// Function to initialize the countdown
function startCountdown() {
  // Get the current date and time
  const currentDate = new Date().getTime();

  // Calculate the time remaining
  const timeRemaining = targetDate - currentDate;

  // TODO: Calculate and update the days, hours, minutes, and seconds
}

Updating the Countdown Timer Display

Now that we have the initial structure and the countdown logic, let’s update the timer display. Inside the startCountdown function, we’ll calculate the remaining days, hours, minutes, and seconds and update the corresponding elements in the HTML. Here’s an example code snippet to help you implement this functionality:

function startCountdown() {
  // Get the current date and time
  const currentDate = new Date().getTime();

  // Calculate the time remaining
  const timeRemaining = targetDate - currentDate;

  // Calculate and update the days, hours, minutes, and seconds
  const daysElement = document.getElementById("days");
  const hoursElement = document.getElementById("hours");
  const minutesElement = document.getElementById("minutes");
  const secondsElement = document.getElementById("seconds");

  // Calculate the days, hours, minutes, and seconds remaining
  let days = Math.floor(timeRemaining / (1000 * 60 * 60 * 24));
  let hours = Math.floor((timeRemaining % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  let minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60));
  let seconds = Math.floor((timeRemaining % (1000 * 60)) / 1000);
  hours = String(hours).padStart(2, "0");
  minutes = String(minutes).padStart(2, "0");
  seconds = String(seconds).padStart(2, "0");
  // Update the HTML elements with the calculated values
  daysElement.textContent = days;
  hoursElement.textContent = hours;
  minutesElement.textContent = minutes;
  secondsElement.textContent = seconds;

  // TODO: Implement the countdown logic to update the timer every second
}

Note: The TODO comment indicates that we still need to implement the countdown logic to update the timer every second. We’ll cover this in the next step.

Adding Additional Functionality

To complete our countdown timer, we need to implement the countdown logic to update the timer every second. We can achieve this by using the setInterval function in JavaScript. Add the following code snippet at the end of the startCountdown function:

// ...

// Update the countdown timer every second
setInterval(startCountdown, 1000);

// ...

This code will call the startCountdown function every second (every 1000 milliseconds), resulting in the countdown timer being updated continuously.

Conclusion

See the Pen Building a Countdown Timer with JavaScript and CSS by BoredNerdy (@TheBoredNerdy) on CodePen 1

Congratulations! You’ve successfully built a countdown timer using JavaScript and CSS. By following this step-by-step guide, you’ve learned how to set up the HTML structure, style the countdown timer using CSS, implement the JavaScript logic, and update display. Feel free to customize the styles and adjust the target date to fit your specific requirements. Now you can enhance your website or web application with an interactive countdown timer that engages your users and creates a sense of urgency.