formatDate(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
const seconds = String(date.getSeconds()).padStart(2, "0");
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
},
getToDay() {
// 获取今天的开始时间和结束时间
const today = new Date();
const todayStartTime = new Date(
today.getFullYear(),
today.getMonth(),
today.getDate(),
0,
0,
0
);
const todayEndTime = new Date(
today.getFullYear(),
today.getMonth(),
today.getDate(),
23,
59,
59
);
return [
this.formatDate(todayStartTime),
this.formatDate(todayEndTime)
];
},
getCurrentWeek() {
// 获取本周的开始时间和结束时间
const currentWeek = new Date();
const currentWeekFirstDay =
currentWeek.getDate() - currentWeek.getDay() + 1;
const currentWeekStartTime = new Date(
currentWeek.getFullYear(),
currentWeek.getMonth(),
currentWeekFirstDay,
0,
0,
0
);
const currentWeekEndTime = new Date(
currentWeek.getFullYear(),
currentWeek.getMonth(),
currentWeekFirstDay + 6,
23,
59,
59
);
return [
this.formatDate(currentWeekStartTime),
this.formatDate(currentWeekEndTime)
];
},
getLastMonth() {
// 获取上月的开始时间和结束时间
const lastMonth = new Date();
lastMonth.setMonth(lastMonth.getMonth() - 1);
const lastMonthFirstDay = new Date(
lastMonth.getFullYear(),
lastMonth.getMonth(),
1
);
const lastMonthStartTime = new Date(
lastMonthFirstDay.getFullYear(),
lastMonthFirstDay.getMonth(),
lastMonthFirstDay.getDate(),
0,
0,
0
);
const lastMonthEndTime = new Date(
lastMonth.getFullYear(),
lastMonth.getMonth() + 1,
0,
23,
59,
59
);
return [
this.formatDate(lastMonthStartTime),
this.formatDate(lastMonthEndTime)
];
},
getLastYera() {
//获取今年的开始和结束时间
const now = new Date(); // 获取当前时间
const year = now.getFullYear(); // 获取当前年份
const startOfYear = new Date(year, 0, 1); // 创建一个新日期对象,表示本年的第一天
const endOfYear = new Date(year, 11, 31, 23, 59, 59, 999); // 创建一个新日期对象,表示本年的最后一天
return [this.formatDate(startOfYear), this.formatDate(endOfYear)];
},
最后输出为数组
["2024-01-09 00:00:00","2024-01-09 23:59:59"]