【问题标题】:Incorrect date countdown日期倒计时不正确
【发布时间】:2017-03-16 19:00:17
【问题描述】:

我正在使用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 brasileiro de verão),结束:2017 年 3 月 15 日星期三 15:11:13 GMT-0300(巴西官方),单位:2047 ,值:-19640926732,千年:0…}

【问题讨论】:

  • 我发现的问题,在 var 单位中,它也得到月份,而不仅仅是天、小时和分钟,问题是我不知道如何取月和取只有天、小时、分钟和秒。

标签: javascript html angularjs angularjs-directive countdown


【解决方案1】:

您正在以可变单位获取所有内容,从而也添加了周和月。 使用单位变量如下:

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

所以他们只会添加天、小时、分钟和秒。

【讨论】:

    【解决方案2】:

    看起来countdownjs 正在按照您的预期工作。

    您选择使用所有单位 (countdown.ALL),因此您应该查看其他单位(您查看天、小时、分钟和秒)。

    Timespan 对象的屏幕截图还显示 7 个月零 1 周。

    如果您想要 total 天数,可以从 Timespan 对象中的毫秒 中获取,例如:

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

    【讨论】:

      【解决方案3】:

      尝试使用所有参数初始化倒计时

      来自countdown.js 文档

      函数倒计时(开始、结束、单位、最大值、位数){

      这样

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

      【讨论】:

      • 错误答案.. 开始和结束混淆了
      猜你喜欢
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多