Search code examples
javascripthtmlangularjsangularjs-directivecountdown

Incorrect date countdown


I'm using the Countdownjs to insert a count in my project but it is returning the wrong day. I'm using AngularJS, here's the directive I created for the count:

.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, I enter the following data:

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

However for this date it is returning 06 days 08 hours 50 min and the resulting seconds. Being that it should actually return more than 100 days.

Active case the timespan console it returns the following given:

n {start: Sun Oct 29 2017 00:00:00 GMT-0200 (Horário brasileiro de verão), end: Wed Mar 15 2017 15:11:13 GMT-0300 (Hora oficial do Brasil), units: 2047, value: -19640926732, millennia: 0…}

inserir a descrição da imagem aqui


Solution

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

    Var units = countdown.DAYS | Countdown.HOURS | Countdown.MINUTES | Countdown.SECONDS;
    

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