I need a countdown timer but since my knowledge is still timid in js
, in that code I do not know how to add "0" in the hour, minute, and second fields.
As it returns me only full numbers (ex: 0: 1: 7). In math.floor
how do I add more decimal places?
Code:
// Set the date we're counting down to
var countDownDate = new Date("Mar 21, 2017 16:35:00").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
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);
// Output the result in an element with id="demo"
document.getElementById("cont").innerHTML = days + " dias " + hours + " : "
+ minutes + " : " + seconds;
// Output the result in an element with id="demo2"
document.getElementById("cont2").innerHTML = " HRS " + " MIN " + " SEG ";
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("cont").innerHTML = "TEMPO EXPIRADA";
}
}, 1000);
#cont {
text-align: center;
font-size: 25px;
font-family: "Oswald", Arial, sans-serif;
}
#cont2 {
text-align: center;
font-size: 15px;
font-family: "Oswald", Arial, sans-serif;
}
<p id="cont"></p>
<p id="cont2"></p>