查询给定时间是否在当前周

直接上代码,是返回true,不是返回false

calTimeWeek(time) {
      // 当前时间
      var timestamp = Date.parse(new Date());
      var serverDate = new Date(timestamp);
      //本周周日的的时间
      var sundayTiem = timestamp + ((7 - serverDate.getDay())* 24 * 60 * 60 * 1000)
      var SundayData = new Date(sundayTiem);
      var tomorrowY = SundayData.getFullYear();
      var tomorrowM = (SundayData.getMonth() + 1 < 10 ? '0' + (SundayData.getMonth() + 1) : SundayData.getMonth() + 1);
      var tomorrowD = SundayData.getDate() < 10 ? '0' + SundayData.getDate() : SundayData.getDate();
      console.log('周日',tomorrowY+'-'+tomorrowM+'-'+tomorrowD);
      // 本周周一的时间
      var mondayTime = timestamp - ((serverDate.getDay()-1) * 24 * 60 * 60 * 1000)
      var mondayData = new Date(mondayTime);
      var mondayY = mondayData.getFullYear();
      var mondayM = (mondayData.getMonth() + 1 < 10 ? '0' + (mondayData.getMonth() + 1) : mondayData.getMonth() + 1);
      var mondayD = mondayData.getDate() < 10 ? '0' + mondayData.getDate() : mondayData.getDate();
      console.log('周一',mondayY + '-' + mondayM + '-' + mondayD);
      var mondayDay = new Date(mondayY + '-' + mondayM + '-' + mondayD+' '+'23:59:59').getTime()   // 周一毫秒数
      var sundayDay = new Date(tomorrowY+'-'+tomorrowM+'-'+tomorrowD+' '+'23:59:59').getTime()   // 周日毫秒数
      var currentDay = new Date(time).getTime()  // 给定时间毫秒数
      if(currentDay > mondayDay){
        if(currentDay < sundayDay){
          return true
        }else{
          return false
        }
      }else{
        return false
      }
    }

猜你喜欢

转载自www.cnblogs.com/Mr-Car/p/11809407.html