Countdown incorrect date

Asked

Viewed 210 times

0

I’m using the Countdownjs to enter a count in my project but it is returning the incorrect day. I am using Angularjs, follows below the directive I created for counting:

.directive('tempoPercorrido', function($interval){
        return {
            link: function(scope, element, attrs){
                var timeNow = new Date(attrs.tempoPercorrido);
                var units = countdown.ALL;
                var timespan = countdown(timeNow, null, units, 0, 0);                    

                function updateTme(){
                    var timespan = countdown(timeNow, null, units, 0, 0);
                    var dias = timespan.days <= 9 ? '0' + timespan.days.toString() : timespan.days.toString();
                    var horas = timespan.hours <= 9 ? '0' + timespan.hours.toString() : timespan.hours.toString();
                    var minutos = timespan.minutes <= 9 ? '0' + timespan.minutes.toString() : timespan.minutes.toString();
                    var segundos = timespan.seconds <= 9 ? '0' + timespan.seconds.toString() : timespan.seconds.toString();

                    var contador = '<div class="dias circulo">'+ dias + '</div>'+
                           '<div class="horas circulo">'+ horas + '</div>'+
                           '<div class="minutos circulo">'+ minutos + '</div>'+
                           '<div class="segundos circulo">'+ segundos + '</div>';
                    //console.log(timespan);
                    $(element).html(contador);       
                }

                updateTme();

                $interval(function(){
                    updateTme();
                }, 1000);
            }
        }
    })

In HTML, insert the following data:

<div class="horario_banner" tempo-percorrido="2017-10-29 00:00:00"></div>

But for this date is returning 06 days 08 hours 50 min and the resulting seconds. In fact it should return more than 100 days.

If active the timespan console it returns the following data:

n {start: Sun Oct 29 2017 00:00:00 GMT-0200 (Brazilian daylight saving time), end: Wed Mar 15 2017 15:11:13 GMT-0300 (Brazilian daylight saving time), Units: 2047, value: -19640926732, millennia: 0...}

inserir a descrição da imagem aqui

  • The problem I found, is in var Units, he is taking the month too, and not only the days, hours, minutes and seconds, the problem is that I do not know how to take the month and take only the days, hours, minutes and seconds.

2 answers

1

You are picking up Everything in the variable Units, causing the week and month to be added up as well. Use the variable Units as follows:

var units = countdown.DAYS | countdown.HOURS | countdown.MINUTES | countdown.SECONDS;

Thus they will add only the days, hours, minutes and seconds.

-1

Test my brother here.. :)

https://www.w3schools.com/howto/howto_js_countdown.asp

<!-- Display the countdown timer in an element -->
<p id="demo"></p>

<script>
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2019 15:37:25").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 and 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);

  // Display the result in the element with id="demo"
  document.getElementById("demo").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";

  // If the count down is finished, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);
</script>
  • Could you explain your code? Often a snippet of code that is trivial to you may not be understood so easily by other users, so a text explanation is always welcome :D

Browser other questions tagged

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