倒计时脚本

时间:2015-03-04 17:33:10

标签: 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');
                            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>

3 个答案:

答案 0 :(得分: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");

作为一个完整的解决方案:

&#13;
&#13;
$(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(){
	var now = moment().format('YYYY-MM-DD HH:mm:ss');
	
	var ms = moment(then,"YYYY-MM-DD HH:mm:ss").diff(moment(now,"YYYY-MM-DD HH:mm:ss"));
	var d = moment.duration(ms);
	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);
	}
	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");			

	if(d.asHours() <= 0) {
		s = "Verlopen";
	}
	$( ".countdown_container_1" ).html( s );
}, 1000);
});
                    
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分: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;

答案 2 :(得分:0)

只有这个部分已经足够了。谢谢

var hours = Math.floor(d.asHours()); var days = Math.floor(hours / 24)//转换为天数。 小时=小时 - 天* 24; //从完整天数的总小时数中减去小时数。 var s = days +“day(s)and”+ hours + moment.utc(ms).format(“:mm:ss”);