2017-12-18 85 views
-1

我有一些每日倒數計時器的代碼,每天計數到中午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> 

回答

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);
<div id="clockdiv"> 
 
    <span class="hours"></span> 
 
    <span class="minutes"></span> 
 
    <span class="seconds"></span> 
 
</div>

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); 
相關問題