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.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.
Very good, but should use the date of arrival to count down for users to enter for convenience