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())
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章