显示当天关闭时间/第二天开放时间的倒计时

问题描述 投票:-2回答:1

我想以小时为单位显示倒计时:分钟:关闭时间(如果打开)或开放时间(关闭时的第二天)的秒数。我怎么能在HTML中这样做?

javascript wordpress
1个回答
0
投票

$(function() {
  var $timer = $('#timer');
  var $countdownText = $('#countdownText');
  var now = moment();
  var openingToday = moment({
    hour: 8
  });
  var closingTime = moment({
    hour: 17
  });
  var openingTime = moment({
    year: now.year(),
    month: now.month(),
    date: now.date() + 1,
    hour: 8
  });
  if (now.diff(openingToday) < 0) {
    $timer.countdown({
      until: openingToday._d
    });
    $countdownText.addClass('closed');
  } else if (now.diff(closingTime) < 0) {
    $timer.countdown({
      until: closingTime._d
    });
    $countdownText.addClass('opened');
  } else {
    $timer.countdown({
      until: openingTime._d
    });
    $countdownText.addClass('closed');
  }
});
#countdownText span {
  display: none;
}

#countdownText.opened .close,
#countdownText.closed .open {
  display: inline;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-countdown/2.0.2/jquery.plugin.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-countdown/2.0.2/jquery.countdown.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-countdown/2.0.2/jquery.countdown.css" />
<div id="countdownText">
  <span class="open">Opening in</span>
  <span class="close">Closing in</span>
</div>
<div id="timer"></div>
© www.soinside.com 2019 - 2024. All rights reserved.