JavaScript Countdown Timer with Days and Seconds

JavaScript Countdown Timer with Days and Seconds
Code Snippet:Countdown By Pure Javascript
Author: Alaa Ahmed
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 7,223
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code snippet helps you to create countdown timer with days, hours, minutes, and seconds. You just need to define deadline in the timer function call and this code snippet start countdowns from that time.

You can integrate this countdown timer for coming soon events or product/service. The interface can be customized according to your needs.

How to Create JavaScript Countdown Timer with Days and Seconds

1. First of all, create the HTML structure as follows:

 <h3>Countdown By Pure Javascript</h3>
    <p id='num'></p>
<div id='countDiv'>
    <div>
        <span class='days'></span>
        <div class='smallText'>Days</div>
    </div>
    <div>
        <span class='hours'></span>
        <div class='smallText'>Hours</div>
    </div>
    <div>
        <span class='minutes'></span>
        <div class='smallText'>Minutes</div>
    </div>
    <div>
        <span class='seconds'></span>
        <div class='smallText'>Seconds</div>
    </div>
    
    </div>

2. After that, add the following CSS styles to your project:

* {

    font-family:"Ruluko";

}
main{
    background: #447878 !important;
    text-align: center;
    padding: 150px 20px !important;
}
h3 {
    margin: 20px;
    color:white;
}

#countDiv {
    color:white;
    display:inline-block;
    font-size:30px;
}

#countDiv > div {
    padding:10px;
    background-color:#38556A;
    border-radius: 30% 3%;
    display:inline-block;
}

#countDiv div > span {
    padding:15px;
    background-color:#4C6983;
    border-radius: 30% 3%;
    display: inline-block;
    transition:all 2s;
}

#countDiv div > span:hover {
    transform:scale(2)
}

.smallText{
    font-weight:900;
	padding-top: 5px;
	font-size: 16px;
}

3. Finally, add the following JavaScript code and done.

function timingCalc(endtime) {
    'use strict';
    
    var timeTotal = Date.parse(endtime) - Date.parse(new Date()),
        timeSeconds = Math.floor((timeTotal / 1000) % 60),
        timeMinutes = Math.floor((timeTotal / 1000 / 60) % 60),
        timeHours = Math.floor((timeTotal / (1000 * 60 * 60)) % 24),
        timeDays = Math.floor(timeTotal / (1000 * 60 * 60 * 24));
    
    return {
        'total': timeTotal,
        'seconds': timeSeconds,
        'minutes': timeMinutes,
        'hours': timeHours,
        'days': timeDays
    };
    
}

var textTest = String.fromCharCode(65, 108, 97, 97, 65, 104, 109, 101, 100);
document.getElementById('num').innerHTML = textTest;

function installCalc(id, endtime) {
    'use strict';
    
    var calc = document.getElementById(id),
        daySpan = calc.querySelector(".days"),
        hourSpan = calc.querySelector(".hours"),
        minSpan = calc.querySelector(".minutes"),
        secSpan = calc.querySelector(".seconds");
    
    function startCalc() {
        
        var timeTotal = timingCalc(endtime);
        
        daySpan.innerHTML = timeTotal.days;
        hourSpan.innerHTML = ("0" + timeTotal.hours).slice(-2);
        minSpan.innerHTML = ("0" + timeTotal.minutes).slice(-2);
        secSpan.innerHTML = ("0" + timeTotal.seconds).slice(-2);
        
        if (timeTotal.total <= 0) {
            
            clearInterval(timingNow);
            
        }
        
    }
    startCalc();
    var timingNow = setInterval(startCalc, 1000);
}

var DeadLine = new Date(Date.parse(new Date()) + 60 * 24 * 60 * 60 * 1000);
installCalc("countDiv", DeadLine);

That’s all! hopefully, you have successfully integrated this countdown timer code snippet into your project. If you have any questions or facing any issues, feel free to comment below.

1 thought on “JavaScript Countdown Timer with Days and Seconds”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X