js 判斷/設置 每天某個時間段執行的函數


不要把它想象的那么難,我們只需要拿到當前【小時】和【分鍾】就行了啊

HandsomeGuy

  以23:30:00 -- 次日 01:00:00 為例

  handle(){

    let hours = new Date().getHours();//拿到當前小時

    let minutes = new Date().getMinutes();//拿到當前分鍾

    if(String(hours) === '23' || String(hours) === '0'){
      // 23:30:00 - 23:59:59 返回true
 
      if( String(hours) === '23' && 29 < Number(minutes) && Number(minutes) < 60){
        return true
 
     // 00:00:00 - 00:29:59 返回true
    }else if( String(hours) === '0' && -1 < Number(minutes) && Number(minutes) < 60){
      return true
    }else{
      return false
      }
    }else{
      return false
    }   
  }

用的時候,直接用這個函數表示狀態就行了

例如:

  const display = this.handle()

<Button disabled = {display}> 時間段限制狀態</Button>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM