微信小程序时间逻辑判断

   最近项目上有个需求,计算请假时间,时间有效范围是09:00:00到17:00:00;最好是去掉周末,很遗憾我这里没有排除掉,新人一枚,把自己做的发出来供大家指正一下,如果有更好的办法欢迎交流讨论;

countTime:function(){
    const that = this
    const startDate = '2020/07/06 17:00:00'
    const endDate = '2020/07/10 09:00:00'
    const date1 = startDate.substring(0,10)
    const date2 = endDate.substring(0,10)
    let hours=0
    if(date1==date2){//日期相等
      let time1
      if(that.compareDate(startDate,date1+' 09:00:00') && !that.compareDate(startDate,date1+' 17:00:00')){//比9点大且比5点小
          time1 = startDate
      }else if(!that.compareDate(startDate,date1+' 09:00:00')){//比9点小
          time1 = date1+' 09:00:00'
      }else{
        time1=0
      }
      
      let time2
      if(!that.compareDate(date2+' 17:00:00',endDate)){//比5点大
        time2 = date2+' 17:00:00'
      }else if(that.compareDate(date2+' 17:00:00',endDate) && !that.compareDate(date2+' 09:00:00',endDate)){//比5点小
        time2 = endDate
      }else{
        time2 = 0
      }
      if(time1==0||time2==0){
        hours = 0
        return
      }
      const start_date = new Date(time1)
      const end_date = new Date(time2)
      let time = (end_date.getTime() - start_date.getTime())/(1000*60*60) 
      console.log(time)
      hours = time.toFixed(2)
    }else{
      var sArr = date1.split("/");
      var eArr = date2.split("/");
      var sRDate = new Date(sArr[0], sArr[1], sArr[2]);
      var eRDate = new Date(eArr[0], eArr[1], eArr[2]);
      var result = (eRDate-sRDate)/(24*60*60*1000);
      if(that.compareDate(date1+' 09:00:00',startDate)){//开始<=9
         if(that.compareDate(date2+' 09:00:00',endDate)){//结束<=9
          hours = (result-1)*8+8
         }else if(!that.compareDate(date2+' 09:00:00',endDate) && !that.compareDate(endDate,date2+' 17:00:00')){//结束>9 && <17
          const start_date = new Date(date2+' 09:00:00')
          const end_date = new Date(endDate)
          let time = (end_date.getTime() - start_date.getTime())/(1000*60*60) 
          hours = (result-1)*8+time+8
         }else if(that.compareDate(endDate,date2+' 17:00:00')){//结束>=17
          hours = (result-1)*8+8+8
         }
      }else if(!that.compareDate(date1+' 09:00:00',startDate) && !that.compareDate(startDate,date1+' 17:00:00')){//开始>9 && 开始<17
        if(that.compareDate(date2+' 09:00:00',endDate)){//结束<=9
          const start_date = new Date(startDate)
          const end_date = new Date(date1+' 17:00:00')
          let time = (end_date.getTime() - start_date.getTime())/(1000*60*60) 
          hours = (result-1)*8+time
       }else if(!that.compareDate(date2+' 09:00:00',endDate) && !that.compareDate(endDate,date2+' 17:00:00')){//结束>9 && <17
        const start_date1 = new Date(startDate)
        const end_date1 = new Date(date1+' 17:00:00')
        let time1 = (end_date1.getTime() - start_date1.getTime())/(1000*60*60) 

        const start_date2 = new Date(date2+' 09:00:00')
        const end_date2 = new Date(endDate)
        let time2 = (end_date2.getTime() - start_date2.getTime())/(1000*60*60) 
        hours = (result-1)*8+time1+time2
       }else if(endDate,date2+' 17:00:00'){//结束>=17
        const start_date1 = new Date(startDate)
        const end_date1 = new Date(date1+' 17:00:00')
        let time1 = (end_date1.getTime() - start_date1.getTime())/(1000*60*60) 
        hours = (result-1)*8+time1+8
       }
      }else if(that.compareDate(startDate,date1+' 17:00:00')){//开始>=17
        if(that.compareDate(date2+' 09:00:00',endDate)){//结束<=9
          hours = (result-1)*8
         }else if(!that.compareDate(date2+' 09:00:00',endDate) && !that.compareDate(endDate,date2+' 17:00:00')){//结束>9 && <17
          const start_date = new Date(date2+' 09:00:00')
          const end_date = new Date(endDate)
          let time = (end_date.getTime() - start_date.getTime())/(1000*60*60)
          hours = (result-1)*8+time
         }else if(that.compareDate(endDate,date2+' 17:00:00')){//结束>=17
          hours = (result-1)*8+8
         }
      }

    }
    return hours
  },
  //日期大小比较
  compareDate(date1,date2){
    var oDate1 = new Date(date1);
    var oDate2 = new Date(date2);
    if(oDate1.getTime() >= oDate2.getTime()){
        return true; //第一个大=
    } else {
        return false; //第二个大
    }
},

遗留问题就是周末的时间的排除,这里找了网上一下大佬的办法:

  loadWeek:function(){
    const startDate = '2020/07/06 17:00:00'
    var dt=new Date(startDate);
    var weekDay = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
    console.log("打印数值---",dt.getDay());
    console.log("今天是:",weekDay[dt.getDay()]);
  },

待完善

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值