js获取某一个时间的上周或者上月的起终点
上周
function getSomeTimesWeekDate(Time = new Date()) {
let weekNum = Time.getDay(); //获取当前是周几
weekNum = weekNum == 0 ? 7 : weekNum;
let lastDate = new Date(Time.getTime() - weekNum * 24 * 60 * 60 * 1000);
let fitstDate = new Date(Time.getTime() - ((weekNum + 6) * 24 * 60 * 60 * 1000));
let startDate =
`${fitstDate.getFullYear()}-${fitstDate.getMonth()+1<10?'0'+(fitstDate.getMonth()+1):fitstDate.getMonth()+1}-${fitstDate.getDate()<10?'0'+fitstDate.getDate():fitstDate.getDate()} 00:00:00`
let endDate =
`${lastDate.getFullYear()}-${lastDate.getMonth()+1<10?'0'+(lastDate.getMonth()+1):lastDate.getMonth()+1}-${lastDate.getDate()<10?'0'+lastDate.getDate():lastDate.getDate()} 23:59:59`
return [startDate, endDate]
}
上个月
function getSomeTimesMonthDate(Time = new Date()) {
let year = Time.getFullYear();
let month = Time.getMonth();
if (month == 0) {
month = 12;
year = year - 1;
}
if (month < 10) {
month = '0' + month;
}
let myDate = new Date(year, month, 0); //上个月的最后一天的开始
let startDate = `${year}-${month}-01 00:00:00`;
let endDate = `${year}-${month}-${myDate.getDate()} 23:59:59`
return [startDate, endDate]
}