134 lines
3.4 KiB
TypeScript
134 lines
3.4 KiB
TypeScript
/**
|
||
* 日期范围计算工具函数
|
||
* 提供从当前日期开始计算本周末、一周内和一个月内的开始和结束日期
|
||
*/
|
||
interface DateRange {
|
||
start: Date;
|
||
end: Date;
|
||
}
|
||
|
||
interface FormattedDateRange {
|
||
start: string;
|
||
end: string;
|
||
}
|
||
|
||
interface AllFormattedRanges {
|
||
thisWeekend: FormattedDateRange;
|
||
nextWeek: FormattedDateRange;
|
||
nextMonth: FormattedDateRange;
|
||
}
|
||
|
||
/**
|
||
* 获取当前日期
|
||
* @returns {Date} 当前日期对象
|
||
*/
|
||
const getCurrentDate = (): Date => {
|
||
return new Date();
|
||
};
|
||
|
||
/**
|
||
* 计算本周末(当前周的周六和周日)
|
||
* @param {Date} [baseDate] 基准日期,默认为当前日期
|
||
* @returns {DateRange} 包含start和end的日期对象
|
||
*/
|
||
const getThisWeekend = (baseDate?: Date): DateRange => {
|
||
const current = baseDate ? new Date(baseDate) : getCurrentDate();
|
||
// 计算到周六的天数(getDay()返回0-6,0是周日,6是周六)
|
||
const daysToSaturday = 6 - current.getDay();
|
||
|
||
// 计算周六日期
|
||
const saturday = new Date(current);
|
||
saturday.setDate(current.getDate() + daysToSaturday);
|
||
|
||
// 计算周日日期
|
||
const sunday = new Date(saturday);
|
||
sunday.setDate(saturday.getDate() + 1);
|
||
|
||
return {
|
||
start: saturday,
|
||
end: sunday
|
||
};
|
||
};
|
||
|
||
/**
|
||
* 计算一周内(从基准日期开始的7天)
|
||
* @param {Date} [baseDate] 基准日期,默认为当前日期
|
||
* @returns {DateRange} 包含start和end的日期对象
|
||
*/
|
||
const getNextWeekRange = (baseDate?: Date): DateRange => {
|
||
const start = baseDate ? new Date(baseDate) : getCurrentDate();
|
||
const end = new Date(start);
|
||
end.setDate(start.getDate() + 6); // 今天 + 6天 = 7天后
|
||
|
||
return {
|
||
start,
|
||
end
|
||
};
|
||
};
|
||
|
||
/**
|
||
* 计算一个月内(从基准日期开始的30天)
|
||
* @param {Date} [baseDate] 基准日期,默认为当前日期
|
||
* @returns {DateRange} 包含start和end的日期对象
|
||
*/
|
||
const getNextMonthRange = (baseDate?: Date): DateRange => {
|
||
const start = baseDate ? new Date(baseDate) : getCurrentDate();
|
||
const end = new Date(start);
|
||
end.setDate(start.getDate() + 29); // 今天 + 29天 = 30天后
|
||
|
||
return {
|
||
start,
|
||
end
|
||
};
|
||
};
|
||
|
||
/**
|
||
* 格式化日期为 YYYY-MM-DD 格式
|
||
* @param {Date} date 日期对象
|
||
* @returns {string} 格式化后的日期字符串
|
||
* @throws {Error} 如果传入的不是有效的日期对象则抛出错误
|
||
*/
|
||
const formatDate = (date: Date): string => {
|
||
if (!(date instanceof Date) || isNaN(date.getTime())) {
|
||
throw new Error('Invalid date object: 请传入有效的Date实例');
|
||
}
|
||
return date.toISOString().split('T')[0];
|
||
};
|
||
|
||
/**
|
||
* 获取所有日期范围的格式化结果
|
||
* @param {Date} [baseDate] 基准日期,默认为当前日期
|
||
* @returns {AllFormattedRanges} 包含所有格式化后的日期范围
|
||
*/
|
||
const getAllFormattedRanges = (baseDate?: Date): AllFormattedRanges => {
|
||
const weekend = getThisWeekend(baseDate);
|
||
const week = getNextWeekRange(baseDate);
|
||
const month = getNextMonthRange(baseDate);
|
||
|
||
return {
|
||
thisWeekend: {
|
||
start: formatDate(weekend.start),
|
||
end: formatDate(weekend.end)
|
||
},
|
||
nextWeek: {
|
||
start: formatDate(week.start),
|
||
end: formatDate(week.end)
|
||
},
|
||
nextMonth: {
|
||
start: formatDate(month.start),
|
||
end: formatDate(month.end)
|
||
}
|
||
};
|
||
};
|
||
|
||
export const dateRangeUtils = {
|
||
getCurrentDate,
|
||
getThisWeekend,
|
||
getNextWeekRange,
|
||
getNextMonthRange,
|
||
formatDate,
|
||
getAllFormattedRanges
|
||
};
|
||
|
||
export default dateRangeUtils;
|