new Date获取时间段
今天、昨天、明天、本周、上周、本月、上月、本季度、上季度、今年、去年、上半年、下半年、最近一周、最近一个月、近三个月
function getTimeFilter(filterType){
let aTypes = {
//今天
Today: function () {
const today = new Date();
let start = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0, 0);
let end = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 23, 59, 59, 999);
return [start, end];
},
//昨天
Yesterday: function () {
const date = new Date();
let start = new Date(date.getFullYear(), date.getMonth(), date.getDate() - 1, 0, 0, 0, 0);
let end = new Date(date.getFullYear(), date.getMonth(), date.getDate() - 1, 23, 59, 59, 999);
return [start, end];
},
//明天
Tomorrow: function () {
const date = new Date();
let start = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1, 0, 0, 0, 0);
let end = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1, 23, 59, 59, 999);
return [start, end];
},
//本周
ThisWeek: function () {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth();
const day = date.getDate();
const week = date.getDay();
let start = new Date(year, month, day - week + 1);
let end = new Date(year, month, day - week + 7);
return [start, end];
},
//上周
LastWeek: function () {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth();
const day = date.getDate();
const week = date.getDay();
let start = new Date(year, month, day - week - 6);
let end = new Date(year, month, day - week);
return [start, end];
},
//本月
ThisMonth: function () {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth();//0-11的数字,也就是说0=1月
let start = new Date(year, month, 1);
let end = new Date(year, month + 1, 0);
return [start, end];
},
//上月
LastMonth: function () {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth();
let start = new Date(year, month - 1, 1);
let end = new Date(year, month, 0);
return [start, end];
},
//本季度:一年分四个季度【0,1,2】【3,4,5】【6,7,8】【9,10,11】
ThisQuarter: function () {
const now = new Date();
const currentMonth = now.getMonth();//0~11【0.1.2】【3.4.5】【6.7.8】【9.10.11】
const currentQuarter = Math.floor(currentMonth / 3) + 1;//当前为8月,month=7,是第3季度
let startMonth = (currentQuarter - 1) * 3;//本季度的开始月份是【6】
let endMonth = currentQuarter * 3 - 1;//本季度的开始月份是【7】
let start = new Date(now.getFullYear(), startMonth, 1);
//结束日期:不好算是30天还是31天,就取下个月的前一天
let end = new Date(now.getFullYear(), endMonth + 1, 0);
return [start, end];
},
//上季度
LastQuarter: function () {
const now = new Date();
const currentMonth = now.getMonth();//0~11【0.1.2】【3.4.5】【6.7.8】【9.10.11】
const currentQuarter = Math.floor(currentMonth / 3) + 1;//当前为8月,month=7,是第3季度
const lastQuarter = currentQuarter - 1;//上季度是第2季度
let startMonth = (lastQuarter - 1) * 3;//上季度的开始月份是【3】
let endMonth = lastQuarter * 3 - 1;//上季度的开始月份是【5】
// 处理年的边界日期【当前正好是第一季的时候,上一季就是0,算出来的startMonth=-2,endMonth=0,实际是上一年的【9.10.11】月】
if (startMonth <= 0) {
startMonth += 11;// startMonth = -2 + 11 = 9
endMonth += 11;// endMonth = 0 + 11 = 11
//如果从上一年开始,则从年中减去1
now.setFullYear(now.getFullYear() - 1);
now.setMonth(startMonth); // 确保月份被重置
}
const start = new Date(now.getFullYear(), startMonth, 1);
//结束日期:不好算是30天还是31天,就取下个月的前一天
const end = new Date(now.getFullYear(), endMonth + 1, 0);
return [start, end];
},
//今年
ThisYear: function () {
const date = new Date();
const year = date.getFullYear();
let start = new Date(year, 0, 1);
let end = new Date(year + 1, 0, 0);
return [start, end];
},
//去年
LastYear: function () {
const date = new Date();
const year = date.getFullYear();
let start = new Date(year - 1, 0, 1);
let end = new Date(year, 0, 0);
return [start, end];
},
//上半年
UpHalfYear: function () {
const date = new Date();
const year = date.getFullYear();
let start = new Date(year, 0, 1);
let end = new Date(year, 6, 0);
return [start, end];
},
//下半年
DownHalfYear: function () {
const date = new Date();
const year = date.getFullYear();
let start = new Date(year, 6, 1);
let end = new Date(year + 1, 0, 0);
return [start, end];
},
//最近一个周:过去的一周
PastOneWeek: function () {
const start = new Date();
start.setTime(start.getTime() - 7 * 24 * 60 * 60 * 1000);
return [start, new Date()];
},
//最近一个月:从今天倒数30天
PastOneMonth: function () {
const start = new Date();
start.setTime(start.getTime() - 30 * 24 * 60 * 60 * 1000);
return [start, new Date()];
},
//最近三个月:从今天倒数90天
PastThreeMonth: function () {
const start = new Date();
start.setTime(start.getTime() - 90 * 24 * 60 * 60 * 1000);
return [start, new Date()];
},
//不限
Unlimited: () => {
return [];
}
};
return filterType ? aTypes[filterType]() : [];
}
//获取今天的时间段
getTimeFilter('Today');
如果需要显示:最近一个月、最近二个月、最近三个月、最近四个月……
优化上面代码,新增方法
function getTimeFilter(filterType, filterNum = null){
if (!filterType) return [];// 如果没有传filterType,那么返回空数组
function calcPastWholeDay(num) {
//计算过去多少整天的起始日期:num是非负整数
// 过去7天: (20240815 11:30) → (20240809 00:00 ~ 20240815 23:59)
// 过去30天:(20240815 11:30) → (20240717 00:00 ~ 20240815 11:30)
// 过去90天:(20240815 11:30) → (20240518 00:00 ~ 20240815 11:30)
if (typeof num !== 'number' || num < 0 || !Number.isInteger(num)) {
throw new Error('无效输入,num必须是非负整数');
}
const date = new Date();
let start = new Date(date.getFullYear(), date.getMonth(), date.getDate() - num, 0, 0, 0, 0);
return [start, new Date()];
}
function calcPastDay(num) {
//计算过去多少天的起始日期:num是非负整数
// 过去7天:(20240815 11:30) → (20240808 11:30 ~ 20240815 11:30)
// 过去30天:(20240815 11:30) → (20240716 11:30 ~ 20240815 11:30)
// 过去90天:(20240815 11:30) → (20240517 11:30 ~ 20240815 11:30)
if (typeof num !== 'number' || num < 0 || !Number.isInteger(num)) {
throw new Error('无效输入,num必须是非负整数');
}
const start = new Date();
start.setTime(start.getTime() - num * 24 * 60 * 60 * 1000);
return [start, new Date()];
}
function calcPastWholeMonth(num) {
//计算过去几个整月:num是非负整数,并且不超过12个月
// 过去3个月: (20240815 11:30) → (20240601 00:00 ~ 20240815 11:30)
// 过去5个月: (20240815 11:30) → (20240301 00:00 ~ 20240815 11:30)
if (typeof num !== 'number' || num < 0 || !Number.isInteger(num)) {
throw new Error('无效输入,num必须是非负整数');
}
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth(); // 注意这里月份是从0开始计数的
let pastMonth = (month - num + 12) % 12; // 确保月份在0到11之间;如果 month - num 是负数,加上12后再取模12,可以确保结果在有效范围内。
let pastYear = (month - num < 0) ? (year - 1) : year; // 根据调整后的月份计算年份
let pastDate = new Date(pastYear, pastMonth, 1);
return [pastDate, date];
}
function calcPastMonth(num) {
//计算过去几个月(固定日期):num是非负整数,并且不超过12个月
// 过去1个月: (20240815 11:30) → (20240715 00:00 ~ 20240815 11:30)
// 过去3个月: (20240815 11:30) → (20240515 00:00 ~ 20240815 11:30)
// 过去6个月: (20240815 11:30) → (20240215 00:00 ~ 20240815 11:30)
if (typeof num !== 'number' || num < 0 || !Number.isInteger(num)) {
throw new Error('无效输入,num必须是非负整数');
}
const date = new Date();
let pastDate = new Date(date.getFullYear(), date.getMonth() - num, date.getDate(), 0, 0, 0, 0);
return [pastDate, date];
}
let aTypes = {
//今天
Today: function () {
const today = new Date();
let start = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0, 0);
let end = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 23, 59, 59, 999);
return [start, end];
},
//昨天
Yesterday: function () {
const date = new Date();
let start = new Date(date.getFullYear(), date.getMonth(), date.getDate() - 1, 0, 0, 0, 0);
let end = new Date(date.getFullYear(), date.getMonth(), date.getDate() - 1, 23, 59, 59, 999);
return [start, end];
},
//明天
Tomorrow: function () {
const date = new Date();
let start = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1, 0, 0, 0, 0);
let end = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1, 23, 59, 59, 999);
return [start, end];
},
//本周
ThisWeek: function () {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth();
const day = date.getDate();
const week = date.getDay();
let start = new Date(year, month, day - week + 1);
let end = new Date(year, month, day - week + 7);
return [start, end];
},
//上周
LastWeek: function () {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth();
const day = date.getDate();
const week = date.getDay();
let start = new Date(year, month, day - week - 6);
let end = new Date(year, month, day - week);
return [start, end];
},
//本月
ThisMonth: function () {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth();//0-11的数字,也就是说0=1月
let start = new Date(year, month, 1);
let end = new Date(year, month + 1, 0);
return [start, end];
},
//上月
LastMonth: function () {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth();
let start = new Date(year, month - 1, 1);
let end = new Date(year, month, 0);
return [start, end];
},
//本季度:一年分四个季度【0,1,2】【3,4,5】【6,7,8】【9,10,11】
ThisQuarter: function () {
const now = new Date();
const currentMonth = now.getMonth();//0~11【0.1.2】【3.4.5】【6.7.8】【9.10.11】
const currentQuarter = Math.floor(currentMonth / 3) + 1;//当前为8月,month=7,是第3季度
let startMonth = (currentQuarter - 1) * 3;//本季度的开始月份是【6】
let endMonth = currentQuarter * 3 - 1;//本季度的开始月份是【7】
let start = new Date(now.getFullYear(), startMonth, 1);
//结束日期:不好算是30天还是31天,就取下个月的前一天
let end = new Date(now.getFullYear(), endMonth + 1, 0);
return [start, end];
},
//上季度
LastQuarter: function () {
const now = new Date();
const currentMonth = now.getMonth();//0~11【0.1.2】【3.4.5】【6.7.8】【9.10.11】
const currentQuarter = Math.floor(currentMonth / 3) + 1;//当前为8月,month=7,是第3季度
const lastQuarter = currentQuarter - 1;//上季度是第2季度
let startMonth = (lastQuarter - 1) * 3;//上季度的开始月份是【3】
let endMonth = lastQuarter * 3 - 1;//上季度的开始月份是【5】
// 处理年的边界日期【当前正好是第一季的时候,上一季就是0,算出来的startMonth=-2,endMonth=0,实际是上一年的【9.10.11】月】
if (startMonth <= 0) {
startMonth += 11;// startMonth = -2 + 11 = 9
endMonth += 11;// endMonth = 0 + 11 = 11
//如果从上一年开始,则从年中减去1
now.setFullYear(now.getFullYear() - 1);
now.setMonth(startMonth); // 确保月份被重置
}
const start = new Date(now.getFullYear(), startMonth, 1);
//结束日期:不好算是30天还是31天,就取下个月的前一天
const end = new Date(now.getFullYear(), endMonth + 1, 0);
return [start, end];
},
//今年
ThisYear: function () {
const date = new Date();
const year = date.getFullYear();
let start = new Date(year, 0, 1);
let end = new Date(year + 1, 0, 0);
return [start, end];
},
//去年
LastYear: function () {
const date = new Date();
const year = date.getFullYear();
let start = new Date(year - 1, 0, 1);
let end = new Date(year, 0, 0);
return [start, end];
},
//上半年
UpHalfYear: function () {
const date = new Date();
const year = date.getFullYear();
let start = new Date(year, 0, 1);
let end = new Date(year, 6, 0);
return [start, end];
},
//下半年
DownHalfYear: function () {
const date = new Date();
const year = date.getFullYear();
let start = new Date(year, 6, 1);
let end = new Date(year + 1, 0, 0);
return [start, end];
},
//最近多少天:今天 + 过去的n-1天
// 近1个周-近7个整天:(20240815 11:30) → (20240809 00:00 ~ 20240815 11:30)
// 近1个月-近30个整天:(20240815 11:30) → (20240717 00:00 ~ 20240815 11:30)
// 近3个月-近90个整天:(20240815 11:30) → (20240518 00:00 ~ 20240815 11:30)
// 近6个月-近180个整天:(20240815 11:30) → (20240218 00:00 ~ 20240815 11:30)
PastNumWholeDay: function (dayNum) {
return calcPastWholeDay(dayNum - 1);
},
//最近多少天:过去的n天
// 近1个周-近7天:(20240815 11:30) → (20240808 11:30 ~ 20240815 11:30)
// 近1个月-近30天:(20240815 11:30) → (20240716 11:30 ~ 20240815 11:30)
// 近3个月-近90天:(20240815 11:30) → (20240517 11:30 ~ 20240815 11:30)
// 近6个月-近180天:(20240815 11:30) → (20240217 11:30 ~ 20240815 11:30)
// 近6个月-近182天:(20240815 11:30) → (20240215 11:30 ~ 20240815 11:30)
PastNumDay: function (dayNum) {
return calcPastDay(dayNum);
},
//最近几个整月:本月 + 过去的n-1个月
//近1个整月:(20240815 11:30) → (20240801 00:00 ~ 20240815 11:30)
//近3个整月:(20240815 11:30) → (20240601 00:00 ~ 20240815 11:30)
//近6个整月:(20240815 11:30) → (20240301 00:00 ~ 20240815 11:30)
PastNumWholeMonth: function (monthNum) {
return calcPastWholeMonth(monthNum - 1);
},
//最近几个月:过去的n个月(按固定日期算)
// 近1个月:(20240815 11:30) → (20240715 00:00 ~ 20240815 11:30)
// 近3个月:(20240815 11:30) → (20240515 00:00 ~ 20240815 11:30)
// 近6个月:(20240815 11:30) → (20240215 00:00 ~ 20240815 11:30)
PastNumMonth: function (monthNum) {
return calcPastMonth(monthNum);
},
//不限
Unlimited: () => {
return [];
}
};
// 方法最开始已经排除了filterType为空值的情况,所以这里不用再判断
//如果不需要处理时间格式,直接返回
// return aTypes[filterType](filterNum) || [];
//如果后端要固定格式的时间段,需要进行格式化,再返回最终时间段
const finalArray = [];
if (aTypes[filterType](filterNum).length > 0) {
aTypes[filterType](filterNum).forEach(date => {
// 按后端所需格式,在此处统一处理【还可以在方法上添加参数来控制不同的时间格式】
finalArray.push(moment(date).format('YYYY-MM-DDTHH:mm:ss.SSS'));
return finalArray;
} else {
return finalArray;
}
}
//getTimeFilter(timeFilter, filterNum);
getTimeFilter('Today'); //今天
getTimeFilter('PastNumWholeDay', 7); //近1个周-近7个整天:(20240815 11:30) → (20240809 00:00 ~ 20240815 11:30)
getTimeFilter('PastNumDay', 7); //近1个周-近7天:(20240815 11:30) → (20240808 11:30 ~ 20240815 11:30)
getTimeFilter('PastNumWholeMonth', 1); //最近一个整月(20240815 11:30) → (20240801 00:00 ~ 20240815 11:30)
getTimeFilter('PastNumMonth', 1); //最近一个月(20240815 11:30) → (20240715 00:00 ~ 20240815 11:30)