将倒计时设置为仅在星期一和星期三之间显示

时间:2017-10-10 11:35:50

标签: javascript

我目前的倒计时时间是每天上午11点59分。

但是,我需要设置它,以便每周下午11.59(星期三)从周一开始计算。

所以周日,周六,周五和周四倒计时不会显示。我已经想到了这一点,但无法找到一种方法来设置我每周三晚上11点59分的倒计时。

我想我会以某种方式使用getDay(),但不知道如何将它放在一起。

这是我目前获得的代码:

var today = new Date(new Date().getTime());
var deadline = new Date(Date.UTC(today.getFullYear(), today.getMonth(), today.getDate(), 10, 59, 59));

function time_remaining(endtime) {
  var t = endtime - 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);
  return {
    'total': t,
    'hours': hours,
    'minutes': minutes,
    'seconds': seconds
  };
}

function run_clock(id, endtime) {
  var clock = document.getElementById(id);
  if (null === clock) {
    return;
  }
  var hours_span = clock.querySelector('.hours');
  var minutes_span = clock.querySelector('.minutes');
  var seconds_span = clock.querySelector('.seconds');

  function update_clock() {
    var t = time_remaining(endtime);
    hours_span.innerHTML = ('0' + t.hours).slice(-2);
    minutes_span.innerHTML = ('0' + t.minutes).slice(-2);
    seconds_span.innerHTML = ('0' + t.seconds).slice(-2);
    day = today.getDay();
    if ((t.total <= 0) || (day === 0) || (day === 6) || (day === 5) || (day === 4)) {
      clearInterval(timeinterval);
      document.getElementById('deadline_Container').style.display = "none";
    }
  }
  update_clock();
  var timeinterval = setInterval(update_clock, 1000);
}
run_clock('clockdiv', deadline);

1 个答案:

答案 0 :(得分:0)

您可以使用getDay()值来获取当天(0(Sunday)...6(Saturday)),然后使用它来显示日历并查找死线

&#13;
&#13;
var today = new Date(),
  curDay = today.getDay();
var deadline = new Date(Date.UTC(today.getFullYear(), today.getMonth(), today.getDate() + 3 - curDay, 10, 59, 59));

//var x = 0;
function time_remaining(endtime) {
  var t = endtime - new Date();
  //var t = endtime - new Date(today).setSeconds(today.getSeconds()+ ++x);
  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));
  //console.log(t, days)
  return {
    'total': t,
    'days': days,
    'hours': hours,
    'minutes': minutes,
    'seconds': seconds
  };
}

function run_clock(id, endtime) {
  var clock = document.getElementById(id);
  if (null === clock) {
    return;
  }
  var days_span = clock.querySelector('.days');
  var hours_span = clock.querySelector('.hours');
  var minutes_span = clock.querySelector('.minutes');
  var seconds_span = clock.querySelector('.seconds');

  function update_clock() {
    var t = time_remaining(endtime);
    days_span.innerHTML = ('0' + t.days).slice(-2);
    hours_span.innerHTML = ('0' + t.hours).slice(-2);
    minutes_span.innerHTML = ('0' + t.minutes).slice(-2);
    seconds_span.innerHTML = ('0' + t.seconds).slice(-2);
    day = today.getDay();
    if ((t.total <= 0) || (day === 0) || (day === 6) || (day === 5) || (day === 4)) {
      clearInterval(timeinterval);
      document.getElementById('deadline_Container').style.display = "none";
    }
  }
  update_clock();
  var timeinterval = setInterval(update_clock, 1000);
}
if (curDay > 0 && today < deadline) {
  run_clock('clockdiv', deadline);
}
&#13;
<div id="deadline_Container"></div>
<div id="clockdiv">
  <span class="days"></span>
  <span class="hours"></span>
  <span class="minutes"></span>
  <span class="seconds"></span>
</div>
&#13;
&#13;
&#13;