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