获取当前日期 前五个自定义周(上周四到本周三为一周)的日期区间:
代码如下:
var that=this,sdate,edate,
currDate=new Date(),
day=currDate.getDay();//0-6 周日-周六
function addDate(date,n){
return new Date(date.getFullYear(), date.getMonth(), date.getDate()+n);
};
function getDayOfWeek (date,g) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1 - g);
}
function formatDate(date){
var year = date.getFullYear(),
m=(date.getMonth()+1),
month = m<10?'0'+m:m,
d=date.getDate(),
day = d<10?'0'+d:d;
return year+'-'+month+'-'+day;
};
//计算第一周的日期
var gap;
if(day==3){
edate=currDate
}else if(day>3){
gap=day-2;
edate=getDayOfWeek(currDate,gap)
}else {
gap=day+5;
edate=getDayOfWeek(currDate,gap)
}
sdate=addDate(edate,-6);
that.weekArr.push({s:formatDate(sdate),e:formatDate(edate)})
//往前循环五周
for(var i=0;i<4;i++){
var nedate=addDate(sdate,-1),
nsdate=addDate(nedate,-6);
that.weekArr.push({s:formatDate(nsdate),e:formatDate(nedate)})
sdate=nsdate;
}
console.log(that.weekArr);
复制代码