1. // 开始日期和结束日期要间隔7天
setDefaultDates() {
const today = new Date();
const endDate = new Date(today.getTime() - 24 * 60 * 60 * 1000); // 结束日期为查询的前一日
const startDate = new Date(endDate.getTime() - 6 * 24 * 60 * 60 * 1000); // 开始日期为结束日期的前一周不包括当天
this.params.endDate = this.formatDate(endDate);
this.params.startDate = this.formatDate(startDate);
},
formatDate(date) {
return date.toISOString().split('T')[0]; // 格式化日期为 yyyy-MM-dd
},
2. // 开始时间和结束时间是当前时间的前一整个月
getLastMonthRange() {
let today = new Date();
let lastMonth = new Date(today.getFullYear(), today.getMonth() - 1, today.getDate());
// 计算上个月的第一天
let firstDayLastMonth = new Date(lastMonth.getFullYear(), lastMonth.getMonth(), 1);
// 计算上个月的最后一天
let lastDayLastMonth = new Date(lastMonth.getFullYear(), lastMonth.getMonth() + 1, 0);
// 格式化日期为 yyyy-MM-dd
let startDate = this.formatDate(firstDayLastMonth);
let endDate = this.formatDate(lastDayLastMonth);
return {
startDate: startDate,
endDate: endDate
};
},
formatDate(date) {
let d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
},
3. // 开始日期和结束日期取当前月份的前一个月(以月份计算的)
setDefaultDates() {
const now = new Date();
const lastMonth = new Date(now.getFullYear(), now.getMonth() - 1, 1);
this.params.startDate = this.formatDate(lastMonth);
this.params.endDate = this.formatDate(lastMonth);
},
formatDate(date) {
return date.getFullYear() + '-' + (date.getMonth() + 1).toString().padStart(2, '0');
},