• Home
  • Guides
    • All
    • Linux
    • Programming
    • Tools
    • WordPress
    My Backup Setup for Linux PCs

    My Backup Setup for Linux PCs

    Detecting Hidden WordPress Malware Disguised as Images

    Detecting Hidden WordPress Malware Disguised as Images

    Server-Side Image Conversion with Apache

    Server-Side Image Conversion with Apache

    Fastest Way to Extract a Massive .tar.gz File on Linux

    Fastest Way to Extract a Massive .tar.gz File on Linux

    Monitor SSL Expiration with Python

    Monitor SSL Expiration with Python

    Building a Simple WordPress Post List Tool with PHP

    Building a Simple WordPress Post List Tool with PHP

    Monitoring Web Page Changes with Python

    Monitoring Web Page Changes with Python

    My SSH Setup: How I Manage Multiple Servers

    My SSH Setup: How I Manage Multiple Servers

    Building a Network Tracker Auditor for Privacy with Python

    Building a Network Tracker Auditor for Privacy with Python

  • Blog
    • All
    • Artificial Intelligence
    • Developer Life
    • Privacy
    • Reviews
    • Security
    • Tutorials
    Imposter Syndrome as a Self-Taught Developer

    Imposter Syndrome as a Self-Taught Developer

    Why Stable Websites Outperform Flashy Redesigns

    Why Stable Websites Outperform Flashy Redesigns

    AdGuard Ad Blocker Review

    AdGuard Ad Blocker Review

    Surfshark VPN Review

    Surfshark VPN Review

    Nmap Unleash the Power of Cybersecurity Scanning

    Nmap: Unleash the Power of Cybersecurity Scanning

    Floorp Browser Review

    Floorp Browser Review

    Understanding Man-in-the-Middle Attacks

    Understanding Man-in-the-Middle Attacks

    Privacy-Focused Analytics

    Privacy-Focused Analytics: Balancing Insights and Integrity

    Safeguarding Your Facebook Account

    Safeguarding Your Facebook Account: Understanding the Differences Between Hacking and Cloning

  • Apps
    • Bible App
    • Bible Verse Screensaver
    • Blue AI Chatbot
    • Early Spring Predictor
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
    • Website Risk Level Tool
  • About
    • About JMooreWV
    • Live Cyber Attack Stats
  • Contact
    • General Contact
    • Website Administration & Cybersecurity
No Result
View All Result
  • Home
  • Guides
    • All
    • Linux
    • Programming
    • Tools
    • WordPress
    My Backup Setup for Linux PCs

    My Backup Setup for Linux PCs

    Detecting Hidden WordPress Malware Disguised as Images

    Detecting Hidden WordPress Malware Disguised as Images

    Server-Side Image Conversion with Apache

    Server-Side Image Conversion with Apache

    Fastest Way to Extract a Massive .tar.gz File on Linux

    Fastest Way to Extract a Massive .tar.gz File on Linux

    Monitor SSL Expiration with Python

    Monitor SSL Expiration with Python

    Building a Simple WordPress Post List Tool with PHP

    Building a Simple WordPress Post List Tool with PHP

    Monitoring Web Page Changes with Python

    Monitoring Web Page Changes with Python

    My SSH Setup: How I Manage Multiple Servers

    My SSH Setup: How I Manage Multiple Servers

    Building a Network Tracker Auditor for Privacy with Python

    Building a Network Tracker Auditor for Privacy with Python

  • Blog
    • All
    • Artificial Intelligence
    • Developer Life
    • Privacy
    • Reviews
    • Security
    • Tutorials
    Imposter Syndrome as a Self-Taught Developer

    Imposter Syndrome as a Self-Taught Developer

    Why Stable Websites Outperform Flashy Redesigns

    Why Stable Websites Outperform Flashy Redesigns

    AdGuard Ad Blocker Review

    AdGuard Ad Blocker Review

    Surfshark VPN Review

    Surfshark VPN Review

    Nmap Unleash the Power of Cybersecurity Scanning

    Nmap: Unleash the Power of Cybersecurity Scanning

    Floorp Browser Review

    Floorp Browser Review

    Understanding Man-in-the-Middle Attacks

    Understanding Man-in-the-Middle Attacks

    Privacy-Focused Analytics

    Privacy-Focused Analytics: Balancing Insights and Integrity

    Safeguarding Your Facebook Account

    Safeguarding Your Facebook Account: Understanding the Differences Between Hacking and Cloning

  • Apps
    • Bible App
    • Bible Verse Screensaver
    • Blue AI Chatbot
    • Early Spring Predictor
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
    • Website Risk Level Tool
  • About
    • About JMooreWV
    • Live Cyber Attack Stats
  • Contact
    • General Contact
    • Website Administration & Cybersecurity
No Result
View All Result
Home Guides Programming JavaScript

Create a Countdown Timer with JavaScript

Jonathan Moore by Jonathan Moore
3 years ago
Reading Time: 6 mins read
A A
Create a Countdown Timer with JavaScript
FacebookTwitter

A countdown timer is a digital timer displayed on a website that counts down to a specific event or deadline. It can be used to build excitement and anticipation for an upcoming event, such as a product launch, a sale, or a new website launch. It can also be used to create a sense of urgency for a limited-time offer or deadline, such as a flash sale or a registration deadline for an event. Countdown timers can be designed in various styles and can include animations, sound effects, and other visual elements to make them more engaging and eye-catching.

Let’s Get Started

Getting started with this project is very easy as we will not be relying on any external libraries, just pure JavaScript.

Create an HTML Page

This is just a basic HTML page without any JavaScript added to it. You are free to design it however you like.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Countdown Timer</title>
</head>
<body>


</body>
</html>

Let’s Add Some CSS Styling

Next, let’s add some CSS styles to give the timer a nice appearance:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Countdown Timer</title>
    <style>
        #countdown {
            display: flex;
            justify-content: center;
            align-items: center;
            font-family: sans-serif;
        }

        .digit-container {
            border: 2px solid black;
            padding: 20px;
            display: inline-block;
            text-align: center;
            font-family: sans-serif;
            width: 80px;
            border-radius: 15px;
            margin: 0 10px;
        }

        .date-num {
            font-size: 3rem;
        }

        .date-text {
            font-size: 1rem;
        }

        .time-sep {
          font-size: 3rem;
        }
    </style>
</head>
<body>


</body>
</html>

This CSS adds some styles to the #countdown DIV, as well as some styles for the individual digit containers, the digit numbers, the text labels, and the time separators.

Add a Container for the Countdown Timer

The countdown timer will need a container so that it can be displayed. We are going to use a DIV container for this with the ID set to countdown as highlighted in line #41.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Countdown Timer</title>
    <style>
        #countdown {
            display: flex;
            justify-content: center;
            align-items: center;
            font-family: sans-serif;
        }

        .digit-container {
            border: 2px solid black;
            padding: 20px;
            display: inline-block;
            text-align: center;
            font-family: sans-serif;
            width: 80px;
            border-radius: 15px;
            margin: 0 10px;
        }

        .date-num {
            font-size: 3rem;
        }

        .date-text {
            font-size: 1rem;
        }

        .time-sep {
          font-size: 3rem;
        }
    </style>
</head>
<body>

    <div id="countdown"></div>

</body>
</html>

Add the JavaScript Code

Now that we have our basic HTML completed, it is now time to finish up our project by adding the remaining JavaScript code underneath our DIV container.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Countdown Timer</title>
    <style>
        #countdown {
            display: flex;
            justify-content: center;
            align-items: center;
            font-family: sans-serif;
        }

        .digit-container {
            border: 2px solid black;
            padding: 20px;
            display: inline-block;
            text-align: center;
            font-family: sans-serif;
            width: 80px;
            border-radius: 15px;
            margin: 0 10px;
        }

        .date-num {
            font-size: 3rem;
        }

        .date-text {
            font-size: 1rem;
        }

        .time-sep {
          font-size: 3rem;
        }
    </style>
</head>
<body>

    <div id="countdown"></div>

    <script type="text/javascript">
        // Set the target date
        var targetDate = new Date("April 30, 2023 23:59:59").getTime();

        // Update the countdown timer every second
        var x = setInterval(function() {
            // Get the current date and time
            var now = new Date().getTime();

            // Calculate the distance between now and the target date
            var distance = targetDate - now;

            // Calculate the number of days, hours, minutes and seconds left
            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);

            // Display the countdown timer with double digits
            document.getElementById("countdown").innerHTML = "<div class='digit-container'><span class='date-num'>" + addZero(days) + "</span><br /><span class='date-text'>Days</span></div> <span class='time-sep'>:</span> <div class='digit-container'><span class='date-num'>" + addZero(hours) + "</span><br /><span class='date-text'>Hours</span></div> <span class='time-sep'>:</span> <div class='digit-container'><span class='date-num'>" + addZero(minutes) + "</span><br /><span class='date-text'>Minutes</span></div> <span class='time-sep'>:</span> <div class='digit-container'><span class='date-num'>" + addZero(seconds) + "</span><br /><span class='date-text'>Seconds</span></div>";

            // If the countdown is over, display "EXPIRED"
            if (distance < 0) {
                clearInterval(x);
                document.getElementById("countdown").innerHTML = "EXPIRED";
            }
        }, 1000);

        // Function to add a leading zero to single-digit numbers
        function addZero(num) {
            if (num < 10) {
                return "0" + num;
            } else {
                return num;
            }
        }
    </script>

</body>
</html>

Our Final Project

And that’s it! You have now added a countdown timer to your website using JavaScript.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Countdown Timer</title>
    <style>
        #countdown {
            display: flex;
            justify-content: center;
            align-items: center;
            font-family: sans-serif;
        }

        .digit-container {
            border: 2px solid black;
            padding: 20px;
            display: inline-block;
            text-align: center;
            font-family: sans-serif;
            width: 80px;
            border-radius: 15px;
            margin: 0 10px;
        }

        .date-num {
            font-size: 3rem;
        }

        .date-text {
            font-size: 1rem;
        }

        .time-sep {
          font-size: 3rem;
        }
    </style>
</head>
<body>

    <div id="countdown"></div>

    <script type="text/javascript">
        // Set the target date
        var targetDate = new Date("April 30, 2023 23:59:59").getTime();

        // Update the countdown timer every second
        var x = setInterval(function() {
            // Get the current date and time
            var now = new Date().getTime();

            // Calculate the distance between now and the target date
            var distance = targetDate - now;

            // Calculate the number of days, hours, minutes and seconds left
            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);

            // Display the countdown timer with double digits
            document.getElementById("countdown").innerHTML = "<div class='digit-container'><span class='date-num'>" + addZero(days) + "</span><br /><span class='date-text'>Days</span></div> <span class='time-sep'>:</span> <div class='digit-container'><span class='date-num'>" + addZero(hours) + "</span><br /><span class='date-text'>Hours</span></div> <span class='time-sep'>:</span> <div class='digit-container'><span class='date-num'>" + addZero(minutes) + "</span><br /><span class='date-text'>Minutes</span></div> <span class='time-sep'>:</span> <div class='digit-container'><span class='date-num'>" + addZero(seconds) + "</span><br /><span class='date-text'>Seconds</span></div>";

            // If the countdown is over, display "EXPIRED"
            if (distance < 0) {
                clearInterval(x);
                document.getElementById("countdown").innerHTML = "EXPIRED";
            }
        }, 1000);

        // Function to add a leading zero to single-digit numbers
        function addZero(num) {
            if (num < 10) {
                return "0" + num;
            } else {
                return num;
            }
        }
    </script>

</body>
</html>

Now that we have our countdown timer working, we can customize it to fit our needs. Here are a few ways to modify:

Change the Target Date and Time

The first thing you might want to do is change the target date and time of the countdown. You can do this by modifying the targetDate variable in the JavaScript code. Just replace the date string with the desired date and time in the format “Month day, year hours:minutes:seconds“. For example, if you want to countdown to December 31st, 2023 at 11:59:59 PM, you can change the targetDate variable to:

var targetDate = new Date("December 31, 2023 23:59:59").getTime();

Customize the Appearance

You can customize the appearance of the countdown timer by modifying the CSS styles. For example, you can change the font family, font size, and color of the countdown timer text. Here’s an example CSS code to change the font family to Arial, font size to 24px, and text color to red:

#countdown {
    font-family: Arial;
    font-size: 24px;
    color: red;
}

Add Sound Effects

You can add sound or animation effects to the countdown timer to make it more engaging. For example, you can add a sound effect when the countdown reaches zero. To add sound to your countdown timer, you can use the HTML5 Audio API to play a sound file at a specific time. Here’s an example of how you can modify the previous code to play a sound file when the countdown reaches zero:

First, add an audio element to your HTML code, like this:

<audio id="countdown-sound" src="path/to/soundfile.mp3"></audio>

Next, add the following code after the document.getElementById(“countdown”).innerHTML = “EXPIRED”; line within the JavaScript:

document.getElementById("countdown-sound").play();

This code checks if the distance is less than zero, which means the countdown has ended, and stops the interval timer. It also changes the innerHTML of the countdown element to “EXPIRED”, and plays the sound file using the play() method on the audio element.

Note that the src attribute of the audio element should point to the location of your sound file.

Tags: CountdownJavaScript
ShareTweetSharePinShareShareScan
ADVERTISEMENT
Jonathan Moore

Jonathan Moore

I am a Software Architect and Senior Software Engineer with 30+ years of experience building applications for Linux and Windows systems. I focus on system architecture, custom web platforms, server infrastructure, and security-focused tools, with an emphasis on performance and reliability. Over the years, I have built everything from WordPress plugins and automation systems to full platforms, ad serving systems, monitoring tools, and API-driven applications. I prefer working close to the system, solving real problems, and building tools that are meant to be used.

Related Articles

Calculating the Date of Easter Using the Computus Algorithm

Calculating the Date of Easter Using the Computus Algorithm

Easter is one of the oldest and most significant festivals in the Christian calendar, celebrating the resurrection of Jesus Christ....

Automating JavaScript Minification with PHP

Automating JavaScript Minification with PHP

Automating JavaScript minification with PHP can significantly streamline a developer's workflow. It ensures that the deployed code is always in...

Synonym Word Replacer

Create a Synonym Word Replacer with JavaScript

Writing compelling and engaging content often requires finding the perfect words to convey our thoughts. However, occasionally we find ourselves...

Next Post
Creating Strong Passwords

Creating Strong Passwords: Factors, Best Practices, and Mistakes to Avoid

Recommended Services

Latest Articles

My Backup Setup for Linux PCs

My Backup Setup for Linux PCs

March 31st is World Backup Day. It is meant to be a reminder, but I do not rely on reminders...

Read moreDetails

Detecting Hidden WordPress Malware Disguised as Images

Detecting Hidden WordPress Malware Disguised as Images

With the recent discovery of malicious files like Stained_Heart_Red-600x500.png being found on servers across the web, it pushed me to...

Read moreDetails

Server-Side Image Conversion with Apache

Server-Side Image Conversion with Apache

I stopped relying on third party image services a while ago. They work, but they add cost, latency, and another...

Read moreDetails

Imposter Syndrome as a Self-Taught Developer

Imposter Syndrome as a Self-Taught Developer

I started writing code over 35 years ago. Everything I learned came from figuring things out on my own, long...

Read moreDetails
  • Privacy Policy
  • Terms of Service

© 2025 JMooreWV. All rights reserved.

No Result
View All Result
  • Home
  • Guides
    • Linux
    • Programming
      • JavaScript
      • PHP
      • Python
    • Tools
    • WordPress
  • Blog
    • Artificial Intelligence
    • Tutorials
    • Privacy
    • Security
  • Apps
    • Bible App
    • Bible Verse Screensaver
    • Blue AI Chatbot
    • Early Spring Predictor
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
    • Website Risk Level Tool
  • About
    • About JMooreWV
    • Live Cyber Attack Stats
  • Contact
    • General Contact
    • Website Administration & Cybersecurity