How do I add decimals to a counter?

Asked

Viewed 143 times

1

I need a regressive counter but as my acquaintances are still more timid in js, in that code I don’t know how to add "0" in the fields hour, minutes and seconds.

As it is returns me only full numbers (ex.: 0 : 1 : 7). In math.floor as 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>

  • Sorry to intrude on the code, but I removed the element p1 that you created and added css to ids of the elements

  • I modified the answer by inserting the time the time expired.

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 inserted a -1* in the calculation to show positive values in time.

  • Perfect! But something that happens is my text below the counter appears aligned to the left even if I define 'center' in the '#cont2' element. How can I force the alignment?

  • because you created an element called p1 that does not exist in the HTML, then you would have to define all the properties of this element for it to work perfectly, as there is a id in your element, use it, will facilitate even reading your code, I’ve made edits to my code now, is 100% functional.

  • I got it. I was able to center it. I’m sorry to abuse it, but one thing is I need the 24-hour counter. In my code I increase the date range it displays the days and hours based on my current date and not using the 24-hour standard. In output cont i removed the day count.

  • You want to display the time that expired and not how long ago it is this?

  • In fact I need to display it like this: if the counter expires tomorrow at 6:00 pm, I display 24:00 pm and counting down.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.