2017-03-15 91 views
2

我正在使用Countdownjs在我的項目中插入一個計數,但它返回錯誤的一天。 我使用AngularJS,這是我爲計數創建的指令:不正確的日期倒計時

.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); 
      } 
     } 
    }) 

在HTML中,我輸入以下數據:

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

但是這個日期它返回06天08小時50分鐘以及由此產生的秒數。 因爲它實際上應該返回超過100天。

活動情況下,它返回的時間跨度控制檯下式給出:

N {開始:太陽2017年10月29日00:00:00 GMT-0200(Horário巴西陸軍德verão),結束:週三2017年3月15日15: 11:13 GMT-0300(霍拉公報做巴西),單位:2047,值:-19640926732,千年:0 ...}

inserir a descrição da imagem aqui

+0

我發現的問題是以var爲單位,它也得到了一個月,而不僅僅是日,小時和分鐘,問題是我不知道如何取月,只取日,小時,分鐘和秒。 –

回答

1

您在變量單位挑選了一切,導致星期和月份也被添加。 使用單位的變量如下:

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

因此,他們將只添加了天,小時,分鐘和秒。

0

嘗試用所有的參數初始化倒計時

countdown.js文檔

功能倒計時(起點,終點,單位,最大值,數字){

像這樣

var today = new Date(); 
function updateTme(){ 
       var timespan = countdown(today , timeNow, units, 0, 0); 
       var dias = ..... 
+0

錯誤的答案..在開始和結束之間感到困惑 –

1

看起來countdownjs正在工作,你打算。

你選擇使用所有單位(countdown.ALL),所以你應該看看你的其他單位(你是只有看着天,小時,分鐘和秒)。

Timespan對象的屏幕截圖還顯示7個月和1周。

如果你想天數,你可以從毫秒在時間跨度對象,例如:

var milliseconds = timespan.value * -1; // Since it's negative in your case 
var seconds = milliseconds/1000; 
var minutes = seconds/60; 
var days = minutes/1440;