Javascript 倒计时脚本

Javascript 倒计时脚本,javascript,countdown,momentjs,Javascript,Countdown,Momentjs,我有一个倒计时脚本,它只显示小时分和秒,就像这样: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');

我有一个倒计时脚本,它只显示小时分和秒,就像这样: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>
解决方案:

这将把你的小时数转换成一个更具可读性的字符串

使用简单的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;//更改此项以使代码段正常工作。将其更改回生产站点的php代码。 window.setIntervalfunction{ var now=moment.format'YYYY-MM-DD HH:MM:ss'; var ms=动量Then,YYYY-MM-DD HH:MM:ss。diffmomentnow,YYYY-MM-DD HH:MM:ss; var d=力矩持续时间ms; 如果d.asHours>96{ 然后=时刻。结束“一天”; ms=momentthen,YYYY-MM-DD HH:MM:ss。diffmomentnow,yyy-MM-DD HH:MM:ss; d=力矩。持续时间ms; } var小时数=Math.floord.asHours; var days=Math.floorhours/24//转换为天。 小时=小时-天*24;//从全天的小时总数中减去小时数。 var s=天+天+小时+时刻.utcms.格式:mm:ss;
如果时间足够的话,只有这一部分已经足够了。谢谢

var小时数=Math.floord.asHours; var days=Math.floorhours/24//转换为天。 小时=小时-天*24;//从全天的小时总数中减去小时数。
var s=days+days and+hours+moment.utcms.format:mm:ss;

哦,算了吧。这只是一个除法的问题。基础数学,这是你高中时学的。把小时除以24,四舍五入,剩下的设为小时。谢谢你的回答,但我对javascript一无所知。谢谢你,但我必须把它放在sc中吗“我觉得有些东西应该删除吗?”安德烈·道达尔补充道。谢谢你,穆瑟,但它仍然不起作用。也许它在倒计时脚本中,它在一个单独的文件中
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");
    // 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;