我有一些日常倒数计时器的代码,每天倒计时到中午12点。我想排除我这样做的周末:
if((t.total<=0) || (day === 0) || (day === 6)){
clearInterval(timeinterval);
document.getElementById('deadline_Container').style.display = "none";
}
}
但是,我还需要在某个日期执行此操作,以便倒计时不会显示在20.12之间。到27.12。例如。不确定如何在特定日期执行此操作?
这是我的全部代码:
<script type="text/javascript">
var today = new Date(new Date().getTime());
var deadline = new Date(Date.UTC(today.getFullYear(),today.getMonth(), today.getDate(), 11, 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)){
clearInterval(timeinterval);
document.getElementById('deadline_Container').style.display = "none";
}
}
update_clock();
var timeinterval = setInterval(update_clock,1000);
}
run_clock('clockdiv',deadline);
</script>
<div id="deadline_Container">
<div id="clockdiv">
<!-- <div><span class="days"></span><span class="smalltext">Days</span></div> -->
<div><span class="hours"></span><span class="smalltext">Hours</span></div>
<div><span class="minutes"></span><span class="smalltext">Minutes</span></div>
<div class="clockLast"><span class="seconds"></span><span class="smalltext">Seconds</span></div>
</div>
</div>
答案 0 :(得分:0)
要检查一个日期是否在另外两个之间,请使用大于且小于:
if(firstDate < actualDate && lastDate > actualDate){
//your code
}
要只检查一个,您可以使用.getTime()或.valueOf()并进行比较:
comparableDate.getTime() == actualDate.getTime()
comparableDate.valueOf() == actualDate.valueOf()
如果您只需要比较日期(而不是时间),我认为.setHours(hour, min, sec, milisec)会有所帮助:
var dateWithoutTime = actualDate;
dateWithoutTime.setHours(0,0,0,0);
答案 1 :(得分:0)
您可以添加扩展条件来检查日期和月份:
const dayOfMonth = today.getDate();
const month = today.getMonth() + 1;
if ((t.total <= 0) || (day === 0) || (day === 6) || (dayOfMonth >= 20 && dayOfMonth <= 27 && month == 12)) {
...
完整的代码是:
var today = new Date(new Date().getTime());
var deadline = new Date(Date.UTC(today.getFullYear(), today.getMonth(), today.getDate(), 11, 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();
const dayOfMonth = today.getDate();
const month = today.getMonth() + 1;
if ((t.total <= 0) || (day === 0) || (day === 6) || (dayOfMonth >= 20 && dayOfMonth <= 27 && month === 12)) {
clearInterval(timeinterval);
document.getElementById('deadline_Container').style.display = "none";
}
}
update_clock();
var timeinterval = setInterval(update_clock, 1000);
}
run_clock('clockdiv', deadline);
&#13;
<div id="clockdiv">
<span class="hours"></span>
<span class="minutes"></span>
<span class="seconds"></span>
</div>
&#13;