巡检日,周,月完成,数据概况待修改

This commit is contained in:
Zy
2026-04-22 16:05:44 +08:00
parent 48852a33ec
commit 04eb40d0e4
55 changed files with 1583 additions and 422 deletions

View File

@@ -4,7 +4,7 @@
* @param date2 结束日期
* @returns 相差天数(绝对值)
*/
export function diffDays(date1: string | Date, date2: string | Date): number {
export function diffDays(date1: string | Date = new Date(), date2: string | Date = new Date()): number {
const d1 = new Date(date1);
const d2 = new Date(date2);
// 转为时间戳并取绝对值
@@ -12,6 +12,28 @@ export function diffDays(date1: string | Date, date2: string | Date): number {
// 一天的毫秒数
return Math.floor(diff / (1000 * 60 * 60 * 24));
}
/**
* 计算两个日期之间的自然日相差天数
* 规则:同一天 = 0昨天 = 1前天 = 2以此类推
* 忽略时分秒,纯日期对比
* @param date1 日期1默认为当前日期
* @param date2 日期2默认为当前日期
* @returns 自然日相差天数(非负整数)
* @example getNaturalDayDiff(今日, 今日) => 0
* @example getNaturalDayDiff(今日, 昨日) => 1
*/
export function getNaturalDayDiff(date1: string | Date = new Date(), date2: string | Date = new Date()): number {
const getDateTimestamp = (date: string | Date) => {
const d = new Date(date);
d.setHours(0, 0, 0, 0);
return d.getTime();
};
const time1 = getDateTimestamp(date1);
const time2 = getDateTimestamp(date2);
return Math.floor(Math.abs(time1 - time2) / (1000 * 60 * 60 * 24));
}
/**
* 格式化两个时间的差值,自动显示 年/月/天/时/分/秒
@@ -134,3 +156,108 @@ export const getLast7Days = (days = 7) => {
}
return dates;
};
/**
* 高性能生成从指定结束日期向前追溯 N 天的连续日期数组
* 规则0=今天1=昨天2=前天,以此类推
* 支持自定义日期格式YYYY/MM/DD/HH/mm/ss或传入格式化函数
* 优化正则模板仅编译1次、时间戳计算、数组预分配无性能损耗
* @category 日期工具
* @param days 向前追溯天数0=今天1=昨天,默认 30 天
* @param endDate 结束日期,支持 Date 对象 / 日期字符串,默认当前日期
* @param format 日期格式化规则,支持模板字符串或自定义格式化函数,默认 'YYYY-MM-DD'
* 支持模板占位符YYYY(年)、MM(月)、DD(日)、HH(时)、mm(分)、ss(秒)
* @returns 格式化后的日期字符串数组(按时间正序排列)
* @example
* // 获取今天
* getLastNDays(0)
* @example
* // 获取昨天
* getLastNDays(1)
* @example
* // 最近7天含今天
* getLastNDays(7)
*/
export function getLastNDays(
days: number = 30,
endDate: Date | string = new Date(),
format: string | ((date: Date) => string) = 'YYYY-MM-DD'
): string[] {
// 非法天数校验
if (!Number.isInteger(days) || days < 0) {
console.warn('getLastNDays: days 必须是大于等于 0 的整数');
return [];
}
// 安全解析结束日期,并【重置时分秒为 00:00:00】— 关键修复
const end = new Date(endDate);
if (isNaN(end.getTime())) {
console.warn('getLastNDays: 无效日期,已自动使用当前时间');
end.setTime(Date.now());
}
// ✅ 修复:统一把时间设为 00:00:00避免跨天计算错误
end.setHours(0, 0, 0, 0);
const endTime = end.getTime();
const oneDay = 86400000;
// 提前编译格式化函数(核心性能优化:正则只编译一次)
let formatter: (date: Date) => string;
if (typeof format === 'function') {
formatter = format;
} else {
formatter = (date: Date) => {
const YYYY = date.getFullYear().toString();
const MM = String(date.getMonth() + 1).padStart(2, '0');
const DD = String(date.getDate()).padStart(2, '0');
const HH = String(date.getHours()).padStart(2, '0');
const mm = String(date.getMinutes()).padStart(2, '0');
const ss = String(date.getSeconds()).padStart(2, '0');
return format.replace(/YYYY/g, YYYY).replace(/MM/g, MM).replace(/DD/g, DD).replace(/HH/g, HH).replace(/mm/g, mm).replace(/ss/g, ss);
};
}
// 生成日期
const result: string[] = [];
for (let i = 0; i <= days; i++) {
const currentDate = new Date(endTime - i * oneDay);
result.push(formatter(currentDate));
}
// 正序返回
return result.reverse();
}
/**
* 格式化 Date 对象为指定格式的字符串
* 高性能正则解析,支持任意分隔符,仅编译一次模板
* @param date 要格式化的日期,默认:当前时间 new Date()
* @param format 格式化模板默认YYYY-MM-DD
* 支持占位符YYYY(年)、MM(月)、DD(日)、HH(时)、mm(分)、ss(秒)
* @returns 格式化后的日期字符串
* @category 日期工具
* @example
* formatDate() // 今天 → 2025-12-20
* @example
* formatDate(new Date(), 'YYYY年MM月DD日')
* @example
* formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss')
*/
export function formatDate(date: Date | string = new Date(), format: string = 'YYYY-MM-DD'): string {
// 解析并校验日期
const d = new Date(date);
if (isNaN(d.getTime())) {
console.warn('formatDate: 无效日期,返回空字符串');
return '';
}
// 获取并补零
const YYYY = d.getFullYear().toString();
const MM = String(d.getMonth() + 1).padStart(2, '0');
const DD = String(d.getDate()).padStart(2, '0');
const HH = String(d.getHours()).padStart(2, '0');
const mm = String(d.getMinutes()).padStart(2, '0');
const ss = String(d.getSeconds()).padStart(2, '0');
// 正则替换(通用、无需穷举)
return format.replace(/YYYY/g, YYYY).replace(/MM/g, MM).replace(/DD/g, DD).replace(/HH/g, HH).replace(/mm/g, mm).replace(/ss/g, ss);
}