本周
function getThisWeek() {
let start = new Date();
let end = new Date();
const now = new Date("2022-02-26 00:00:00");
let day = now.getDay();
console.log(day)
let minusDay = day !== 0 ? day - 1 : 6;
start.setTime(now.getTime() - 3600 * 1000 * 24 * minusDay);
end.setTime(now.getTime() + 3600 * 1000 * 24 * (6 - minusDay));
start.setHours(0, 0, 0);
end.setHours(23, 59, 59);
return start.toLocaleDateString() + " - " + end.toLocaleDateString();
}
// 获取最近一周
function getRecentWeek() {
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 6);
return start.toLocaleDateString() + " - " + end.toLocaleDateString();
}
console.log(getRecentWeek());
获取上个月
function getPreviousMonth() {
let start = null;
let end = null;
const now = new Date('2023/1/2 00:00:00');
let year = now.getFullYear();
let month = now.getMonth();
if (month === 0) {
month = 12;
year = year - 1;
}
if (month < 10) {
month = "0" + month;
}
const yDate = new Date(year, month, 0);
start = new Date( year + '-' + month + '-' + '01 00:00:00'); //上个月第一天
end = new Date( year + '-' + month + '-' + yDate.getDate() + ' 23:59:59'); //上个月最后一天
return start.toLocaleDateString() + " - " + end.toLocaleDateString();
}
console.log(getPreviousMonth())