js获取当前日的前三天,除去周末

1.获取当前日期

 currentData(){//当前日期
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth()+1;
    let time = date.getDate();
    month < 10? `0${month}` : month
    time < 10? `0${time}` :time
    return `${year}-${month}-${time}`
 }

2.检查是否为周末

checkWeeks(date){ //检查是否为周末
   //0-周天,6-周六
  let isWeeks = new Date(date).getDay()
  if (isWeeks == 0 || isWeeks == 6) {
     return false; //不是返回False
   }else {
     return true;
   }
}

3.当前日的前三天,除去周末

dateDesc(startDate){
   let i = 0, lastStartDate,lastArr = [];
   startDate = new Date(startDate);
   while( i < 3) {
    startDate = +startDate - 1000*60*60*24;
    startDate = new Date(startDate);
    let startMonth = (startDate.getMonth()+1) < 10? `0${startDate.getMonth()+1}`:    		startDate.getMonth()+1
    let startTime = startDate.getDate() < 10? `0${startDate.getDate()}`: startDate.getDate()
    lastStartDate = startDate.getFullYear()+"-"+startMonth+"-"+startTime;
    if(this.checkWeeks(lastStartDate)){
        i++
        lastArr.push(lastStartDate)
     }
        continue
   }
    return lastArr
}

4.获取前三天时间,以数组格式返回

 let lastDateArr = this.dateDesc(this.currentData())

你可能感兴趣的:(javascript)