jquery将秒转换为此格式

时间:2017-08-08 11:33:32

标签: javascript php jquery timer

嗨我安装了一个插件,其中有一个倒数计时器:

function getTimeRemaining(endtime) {
 var t = Date.parse(endtime) - Date.parse(new Date());
      var seconds = Math.floor((t / 1000) % 60);
      var minutes = Math.floor((t / 1000 / 60) % 60);
      var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
      var days = Math.floor(t / (1000 * 60 * 60 * 24));
      return {
        'total': t,
        'days': days,
        'hours': hours,
        'minutes': minutes,
        'seconds': seconds
      };
    }

    function initializeClock(id, endtime) {
      var clock = document.getElementById(id);
      var daysSpan = clock.querySelector('.days');
      var hoursSpan = clock.querySelector('.hours');
      var minutesSpan = clock.querySelector('.minutes');
      var secondsSpan = clock.querySelector('.seconds');

      function updateClock() {
        var t = getTimeRemaining(endtime);
        daysSpan.innerHTML = t.days;
        hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
        minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
        secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);

        if (t.total <= 0) {
          clearInterval(timeinterval);
        }
      }
      updateClock();
      var timeinterval = setInterval(updateClock, 1000);
    }

    var deadline = new Date(Date.parse(new Date()) +  1 * 60 * 60 * 1000);
    initializeClock('clockdiv', deadline);

我有一个基于秒运行的倒数计时器代码..

所以我想用上面的代码格式编写我的代码,在几秒钟内完成截止日期

var gdtesttime  = 00:15:00;         //hh:mm:ss
var a = gdtesttime.split(':');      // split it at the colons
var gdseconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 ;  //seconds

我需要这两条主线的帮助:

1 var deadline = new Date(Date.parse(new Date()) + 1 * 60 * 60 * 1000);

2 var t = Date.parse(endtime) - Date.parse(new Date());

这是我的HTML:

<div id="clockdiv" class="pull-right">
    <span><img src="assets/images/timer.png" /></span>
    <div class="disp-none">
        <span class="days"></span>
        <div class="smalltext">Days</div>
    </div>
    <div>
        <span class="hours"></span><span>:</span>
    </div>
    <div>
        <span class="minutes"></span><span>:</span>
    </div>
    <div>
        <span class="seconds"></span>
    </div>
</div>

JS小提琴:https://jsfiddle.net/k4b81x8c/

1 个答案:

答案 0 :(得分:1)

如果您添加var gdtesttime = 00:15:00;,则必须在"'引号中提供此时间,否则您在js中有错误。在转换gdseconds时最后也应用* 1000,最后将其添加到死线。因此,请将脚本更新为:

<script type="text/javascript">
  function getTimeRemaining(endtime) {
 var t = Date.parse(endtime) - Date.parse(new Date());
      var seconds = Math.floor((t / 1000) % 60);
      var minutes = Math.floor((t / 1000 / 60) % 60);
      var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
      var days = Math.floor(t / (1000 * 60 * 60 * 24));
      return {
        'total': t,
        'days': days,
        'hours': hours,
        'minutes': minutes,
        'seconds': seconds
      };
    }

    function initializeClock(id, endtime) {
      var clock = document.getElementById(id);
      var daysSpan = clock.querySelector('.days');
      var hoursSpan = clock.querySelector('.hours');
      var minutesSpan = clock.querySelector('.minutes');
      var secondsSpan = clock.querySelector('.seconds');

      function updateClock() {
        var t = getTimeRemaining(endtime);
        daysSpan.innerHTML = t.days;
        hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
        minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
        secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);

        if (t.total <= 0) {
          clearInterval(timeinterval);
        }
      }
      updateClock();
      var timeinterval = setInterval(updateClock, 1000);
    }
    var gdtesttime  = "00:15:00";         //hh:mm:ss
    var a = gdtesttime.split(':');      // split it at the colons
    var gdseconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 * 1000;  //miliseconds
    var deadline = new Date(Date.parse(new Date()) + gdseconds);
    initializeClock('clockdiv', deadline);
</script>

检查js小提琴:

https://jsfiddle.net/k4b81x8c/1/