Created
December 13, 2013 18:26
-
-
Save rorhug/7948852 to your computer and use it in GitHub Desktop.
Countdown with padded seconds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// set the date we're counting down to | |
var target_date = new Date("Aug 15, 2019").getTime(); | |
// variables for time units | |
var days, hours, minutes, seconds; | |
// get tag element | |
var countdown = document.getElementById("countdown"); | |
// update the tag with id "countdown" every 1 second | |
setInterval(function () { | |
// find the amount of "seconds" between now and target | |
var current_date = new Date().getTime(); | |
var seconds_left = (target_date - current_date) / 1000; | |
// do some time calculations | |
days = parseInt(seconds_left / 86400); | |
seconds_left = seconds_left % 86400; | |
hours = parseInt(seconds_left / 3600); | |
seconds_left = seconds_left % 3600; | |
minutes = parseInt(seconds_left / 60); | |
seconds = parseInt(seconds_left % 60); | |
//NEW CODE | |
var secondsString = seconds.toString(); | |
// format countdown string + set tag value | |
countdown.innerHTML = days + "d, " + hours + "h, " | |
+ minutes + "m, " + (secondsString.length > 1 ? secondsString : "0" + secondsString) + "s"; | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment