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())