How do I add decimal places in a counter?

1

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>
    
asked by anonymous 21.03.2017 / 20:37

1 answer

0

A simple way to solve your problem would be to put something like

days = days < 10 ? "0" + days : days;
hours = hours< 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;

Then it would look like this:

// Set the date we're counting down to
    var date = new Date("Mar 23, 2017 18:00:00");
    var countDownDate = date.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));

        // If the count down is over, write some text 
        if (distance < 0) {
            document.getElementById("cont").innerHTML = "TEMPO EXPIRADO";
            clearInterval(x);
            
            var year = date.getFullYear();
            var month = date.getMonth()+1;
            var day = date.getDay()+1;
            hours = date.getHours();
            minutes = date.getMinutes() ;
            seconds = date.getSeconds();
            
            year = year <= 9 ? "0" + year : year;
            month = month <= 9 ? "0" + month : month;
            day = day <= 9 ? "0" + day : day;
            hours = hours <= 9 ? "0" + hours : hours;
            minutes = minutes <= 9 ? "0" + minutes : minutes;
            seconds = seconds <= 9 ? "0" + seconds : seconds;

            // Output the result in an element with id="demo2"
            document.getElementById("cont2").innerHTML = day + "/" + month + "/" + year + " " + hours + ":" + minutes + ":" + seconds;
        } else {
            days = days <= 9 ? "0" + days : days;
            days = days == 1 ? days + " dia " : days + " dias ";
            hours = hours <= 9 ? "0" + hours : hours;
            minutes = minutes <= 9 ? "0" + minutes : minutes;
            seconds = seconds <= 9 ? "0" + seconds : seconds;
        
            // Output the result in an element with id="demo"
            document.getElementById("cont").innerHTML = days + hours + ":"
            + minutes + ":" + seconds;
        }
    }, 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>

I entered a -1* in the calculation, so that positive values are shown in time.

    
21.03.2017 / 20:50