2015-03-04 4 views
-4

У меня есть Wich обратный отсчет скрипт только показывает часы минуты и секунды, как это: 29:30 54Обратный отсчет сценарий

Но я хочу, чтобы он, как этот 1 день и 4:30:54

Этот это сценарий, что я должен изменить я попробовал несколько вещей, но он хочет работать:

<script type="text/javascript"> 
         $(function() { 
          window.setInterval(function(){ 
          var now = moment().format('YYYY-MM-DD HH:mm:ss'); 
          var then = "<?php echo $d['expiration'] ?>"; 
          var ms = moment(then,"YYYY-MM-DD HH:mm:ss").diff(moment(now,"YYYY-MM-DD HH:mm:ss")); 
          var d = moment.duration(ms); 
          var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss"); 
          if(d.asHours() > 96) { 
           then = moment().endOf('day'); 
           ms = moment(then,"YYYY-MM-DD HH:mm:ss").diff(moment(now,"YYYY-MM-DD HH:mm:ss")); 
           d = moment.duration(ms); 
           s = Math.floor(d()) + moment.utc(ms).format(":mm:ss"); 
          } 
          if(d.asHours() <= 0) { 
           s = "Verlopen"; 
          } 
          $(".countdown_container_1").html(s); 
          }, 1000); 
         }); 
        </script> 
        <div class="countdown_container countdown_container_1">0:00:00</div> 
+1

О, давай .. Это всего лишь вопрос разделов .. – briosheje

+0

Базовая математика, вы узнали об этом в старшей школе. Разделите часы на 24, округлите его и установите остаток в часах. – Mouser

+0

Благодарим вас за ответы, но я ничего не знаю о javascript. – Andre

ответ

0

решение here:

d = moment.duration(Math.floor(d.asHours()), 'hours').humanize(); 

Это должно преобразовать ваши часы в более понятную для человека строку.

Выполнение этой задачи с использованием простого JavaScript.

var hours = Math.floor(d.asHours()); 
var days = Math.floor(hours/24) //convert to days. 
hours = hours - days*24; //subtract the amount of hours from the total amount of hours that are full days. 
var s = days + " day(s) and " + hours + moment.utc(ms).format(":mm:ss"); 

И как общее решение:

$(function() { 
 

 
var then = "2015/03/7"; //changed this to make the snippet work. Change it back to the php code for your production site. 
 
window.setInterval(function(){ 
 
\t var now = moment().format('YYYY-MM-DD HH:mm:ss'); 
 
\t 
 
\t var ms = moment(then,"YYYY-MM-DD HH:mm:ss").diff(moment(now,"YYYY-MM-DD HH:mm:ss")); 
 
\t var d = moment.duration(ms); 
 
\t if(d.asHours() > 96) { 
 
\t \t then = moment().endOf('day'); 
 
\t \t ms = moment(then,"YYYY-MM-DD HH:mm:ss").diff(moment(now,"YYYY-MM-DD HH:mm:ss")); 
 
\t \t d = moment.duration(ms); 
 
\t } 
 
\t var hours = Math.floor(d.asHours()); 
 
\t var days = Math.floor(hours/24) //convert to days. 
 
\t hours = hours - days*24; //subtract the amount of hours from the total amount of hours that are full days. 
 
\t var s = days + " day(s) and " + hours + moment.utc(ms).format(":mm:ss"); \t \t \t 
 

 
\t if(d.asHours() <= 0) { 
 
\t \t s = "Verlopen"; 
 
\t } 
 
\t $(".countdown_container_1").html(s); 
 
}, 1000); 
 
}); 
 
       
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><script src="http://momentjs.com/downloads/moment.js"></script> 
 

 

 
<div class="countdown_container countdown_container_1">0:00:00</div>

+0

Спасибо, но мне нужно было поместить его в скрипт, и я думаю, что некоторые вещи нужно удалить? – Andre

+0

@ Добавлен общее решение для Android. – Mouser

+0

Спасибо, Mouser, но не работает. Возможно, это в сценарии обратного отсчета, который находится в отдельном файле – Andre

0
// Generated by CoffeeScript 1.4.0 

/* 
countdown is a simple jquery plugin for countdowns 

Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
and GPL-3.0 (http://opensource.org/licenses/GPL-3.0) licenses. 

@source: http://github.com/rendro/countdown/ 
@autor: Robert Fleischmann 
@version: 1.0.1 
*/ 


(function() { 

    (function($) { 
    $.countdown = function(el, options) { 
     var getDateData, 
     _this = this; 
     this.el = el; 
     this.$el = $(el); 
     this.$el.data("countdown", this); 
     this.init = function() { 
     _this.options = $.extend({}, $.countdown.defaultOptions, options); 
     if (_this.options.refresh) { 
      _this.interval = setInterval(function() { 
      return _this.render(); 
      }, _this.options.refresh); 
     } 
     _this.render(); 
     return _this; 
     }; 
     getDateData = function(endDate) { 
     var dateData, diff; 
     endDate = Date.parse($.isPlainObject(_this.options.date) ? _this.options.date : new Date(_this.options.date)); 
     diff = (endDate - Date.parse(new Date))/1000; 
     if (diff <= 0) { 
      diff = 0; 
      if (_this.interval) { 
      _this.stop(); 
      } 
      _this.options.onEnd.apply(_this); 
     } 
     dateData = { 
      years: 0, 
      days: 0, 
      hours: 0, 
      min: 0, 
      sec: 0, 
      millisec: 0 
     }; 
     if (diff >= (365.25 * 86400)) { 
      dateData.years = Math.floor(diff/(365.25 * 86400)); 
      diff -= dateData.years * 365.25 * 86400; 
     } 
     if (diff >= 86400) { 
      dateData.days = Math.floor(diff/86400); 
      diff -= dateData.days * 86400; 
     } 
     if (diff >= 3600) { 
      dateData.hours = Math.floor(diff/3600); 
      diff -= dateData.hours * 3600; 
     } 
     if (diff >= 60) { 
      dateData.min = Math.floor(diff/60); 
      diff -= dateData.min * 60; 
     } 
     dateData.sec = diff; 
     return dateData; 
     }; 
     this.leadingZeros = function(num, length) { 
     if (length == null) { 
      length = 2; 
     } 
     num = String(num); 
     while (num.length < length) { 
      num = "0" + num; 
     } 
     return num; 
     }; 
     this.update = function(newDate) { 
     _this.options.date = newDate; 
     return _this; 
     }; 
     this.render = function() { 
     _this.options.render.apply(_this, [getDateData(_this.options.date)]); 
     return _this; 
     }; 
     this.stop = function() { 
     if (_this.interval) { 
      clearInterval(_this.interval); 
     } 
     _this.interval = null; 
     return _this; 
     }; 
     this.start = function(refresh) { 
     if (refresh == null) { 
      refresh = _this.options.refresh || $.countdown.defaultOptions.refresh; 
     } 
     if (_this.interval) { 
      clearInterval(_this.interval); 
     } 
     _this.render(); 
     _this.options.refresh = refresh; 
     _this.interval = setInterval(function() { 
      return _this.render(); 
     }, _this.options.refresh); 
     return _this; 
0

Только этот Npart был alreadyh достаточно. Спасибо

var hours = Math.floor (d.asHours()); var days = Math.floor (hours/24) // конвертировать в дни. часов = часы - дни * 24; // вычитаем количество часов от общего количества часов, которые являются полными днями. var s = days + "day (s)" и "+ hours + moment.utc (ms) .format (": mm: ss ");

Смежные вопросы