You are currently viewing Responsive Countdown to a Certain Date With JavaScript

Responsive Countdown to a Certain Date With JavaScript

Responsive Countdown to a Certain Date With JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make a Responsive Countdown maker for a certain date which will be entered by the user and the deadline will be terminated on that day. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Responsive Countdown to a Certain Date Project.

The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make a Responsive Countdown to a Certain Date Project. Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Responsive Countdown to a Certain Date Project. At last, we will use JS (JavaScript) which will add logic to make the Responsive Countdown to a Certain Date Project responsive from the user end.

In this blog post, we will discuss Countdown to a Certain Date with JavaScript complete source code so you can just copy and paste them into your own project. Happy exploring and learning !!

I hope you have got an idea about the project.

HTML Code for Responsive Countdown

First, we’ll start with creating the structure of the Responsive Countdown to a Certain Date project for that as you can see in the above code we have used all the necessary elements & attributes to set up the structure. Let us know code the CSS part to add styling and aligned the tags.

<html lang="en">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Countdown</title>
    <!-- Google Font -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="countdown">
        <div class="box">
            <span class="num" id="day-box">00</span>
            <span class="text">Days</span>
        </div>
        <div class="box">
            <span class="num" id="hr-box">00</span>
            <span class="text">Hours</span>
        </div>
        <div class="box">
            <span class="num" id="min-box">00</span>
            <span class="text">Minutes</span>
        </div>
        <div class="box">
            <span class="num" id="sec-box">00</span>
            <span class="text">Seconds</span>
        </div>
    </div>

    <!-- Script -->
    <script src="script.js"></script>
</body>
</html>

Chatbot UI Template Design Using HTML And CSS

CSS Code for Responsive Countdown

Second, comes the CSS code which we have styled for the structure we have padded as well as aligned the Responsive Countdown to a Certain Date project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now, let’s code the JavaScript part to make it responsive.

*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: "Poppins",sans-serif;
    color: #ffffff;
}
body{
    background-color: #111111;
}
.countdown{
    width: 80vw;
    display: flex;
    justify-content: space-around;
    gap: 10px;
    position: absolute;
    transform: translate(-50%,-50%);
    top: 50%;
    left: 50%;
}
.box{
    width: 28vmin;
    height: 28vmin;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-evenly;
    position: relative;
    box-shadow: 15px 15px 30px rgba(0,0,0,0.5);
    font-size: 16px;
}
.box:after{
    content: "";
    position: absolute;
    background-color: rgba(255,255,255,0.12);
    height: 100%;
    width: 50%;
    left: 0;
}
span.num{
    background-color: #202020;
    height: 100%;
    width: 100%;
    display: grid;
    place-items: center;
    font-weight: 600;
    font-size: 5em;
}
span.text{
    font-size: 1.2em;
    background-color: #2887e3;
    display: block;
    width: 100%;
    text-align: center;
    padding: 0.5em 0;
    font-weight: 400;
}
@media screen and (max-width: 1024px){
    .countdown{
        width: 85vw;
    }
    .box{
        height: 26vmin;
        width: 26vmin;
        font-size: 12px;
    }
}
@media screen and (max-width: 768px){
    .countdown{
        width: 90vw;
        flex-wrap: wrap;
        gap: 30px;
    }
    .box{
        width: calc( 50% - 40px );
        height: 30vmin;
        font-size: 14px;
    }
}
@media screen and (max-width: 480px){
    .countdown{
        gap: 15px;
    }
    .box{
        width: 100%;
        height: 25vmin;
        font-size: 8px;
    }
    .span.text{
        font-size: 1.5em;
    }
}

Search Keyword Highlighting Text Using JavaScript

JavaScript Code for Responsive Countdown

Last stage of the project the JavaScript which we added the logic and coded as per the requirement with some conditions. Let us see the Final Output of the project Responsive Countdown to a Certain Date using HTML, CSS & JavaScript.

let dayBox = document.getElementById("day-box");
let hrBox = document.getElementById("hr-box");
let minBox = document.getElementById("min-box");
let secBox = document.getElementById("sec-box");

//Format: Date(year, month, day, hour, minute)
//Year is counter from 0 to 11
let endDate = new Date(2021,8,5,16,30);
//Output value in milliseconds
let endTime = endDate.getTime();

function countdown(){
    let todayDate = new Date();
    //Output value in milliseconds
    let todayTime = todayDate.getTime();

    let remainingTime = endTime - todayTime;

    //60sec => 1000 milliseconds
    let oneMin = 60 * 1000;
    //1hr => 60 minutes
    let oneHr = 60 * oneMin;
    //1 day => 24 hours
    let oneDay = 24 * oneHr;

    //Function to format number if it is single digit
    let addZeroes = num => num < 10 ? `0${num}` : num;

    //If end dat is before today date
    if(endTime < todayTime){
        clearInterval(i);
        document.querySelector(".countdown").innerHTML = `<h1>Countdown had expired!</h1>`;
    }
    //If end date is not before today date
    else{
        //Calculating remaining days, hrs,mins ,secs
        let daysLeft = Math.floor(remainingTime / oneDay);
        let hrsLeft = Math.floor((remainingTime % oneDay) / oneHr);
        let minsLeft = Math.floor((remainingTime % oneHr) / oneMin);
        let secsLeft = Math.floor((remainingTime % oneMin) / 1000);

        //Displaying Valurs
        dayBox.textContent = addZeroes(daysLeft);
        hrBox.textContent = addZeroes(hrsLeft);
        minBox.textContent = addZeroes(minsLeft);
        secBox.textContent = addZeroes(secsLeft);
    }
}
let i = setInterval(countdown,1000);
countdown();

Final Output

We have successfully created our Responsive Countdown to a Certain Date using HTML, CSS & JavaScript. You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search codewithrandom on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Thank You And Keep Learning!!!

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9

Leave a Reply