只在特定日期后运行javascript功能
我有一些每日倒数计时器的代码,每天计数到中午12点。我想排除我已经这样做周末:只在特定日期后运行javascript功能
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>
回答:
您可以添加一个扩展状态检查日,月:
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);
<div id="clockdiv"> <span class="hours"></span>
<span class="minutes"></span>
<span class="seconds"></span>
</div>
回答:
要检查是否一个日期是其他两个之间,使用比做大小于:
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);
以上是 只在特定日期后运行javascript功能 的全部内容, 来源链接: utcz.com/qa/262684.html