/** * 格式化日期对象为字符串 * @param {Date} date - 日期对象 * @returns {string} - 格式化后的日期字符串,格式为 YYYY-MM-DD HH:mm:ss */ const formatDate = (date: Date) => { const pad = (num: number) => (num < 10 ? "0" + num : num) return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}` } /** * 将日期字符串解析为日期对象 * @param {string} dateStr - 日期字符串,格式为 YYYY-MM-DD HH:mm:ss * @returns {Date} - 解析后的日期对象 */ const parseDate = (dateStr: string) => { const [datePart, timePart] = dateStr.split(" ") const [year, month, day] = datePart.split("-").map(Number) const [hours, minutes, seconds] = timePart.split(":").map(Number) return new Date(year, month - 1, day, hours, minutes, seconds) } /** * 获取指定时间范围的开始时间和结束时间 * @param {("daily" | "weekly" | "threeDays")} timeScope - 时间范围,可以是 "daily"、"weekly" 或 "threeDays" * @returns {{ startTime: string, endTime: string }} - 格式化后的开始时间和结束时间 */ export const getTimeRange = (timeScope: "daily" | "weekly" | "threeDays") => { const now = new Date() const startTime = new Date(now) startTime.setHours(0, 0, 0, 0) const endTime = new Date(now) endTime.setHours(23, 59, 59, 999) if (timeScope === "weekly") { const day = now.getDay() const diff = day === 0 ? 6 : day - 1 // adjust when day is sunday startTime.setDate(now.getDate() - diff) } else if (timeScope === "threeDays") { startTime.setDate(now.getDate() - 2) } return { startTime: formatDate(startTime), endTime: formatDate(endTime), } } /** * 调整时间范围 * 如果开始时间晚于结束时间,则交换两者 * 如果开始时间和结束时间都晚于当前时间,则调整为过去三天 * 如果开始时间早于当前时间且结束时间晚于当前时间,则将结束时间调整为当前时间 * @param {string} startTime - 开始时间,格式为 YYYY-MM-DD HH:mm:ss * @param {string} endTime - 结束时间,格式为 YYYY-MM-DD HH:mm:ss * @returns {{ startTime: string, endTime: string }} - 调整后的开始时间和结束时间 */ export const adjustTimeRange = (startTime: string, endTime: string) => { const now = new Date() let start = parseDate(startTime) let end = parseDate(endTime) if (start > end) { ;[start, end] = [end, start] } if (start > now && end > now) { return getTimeRange("threeDays") } else if (start < now && end > now) { end = now end.setHours(23, 59, 59, 999) } return { startTime: formatDate(start), endTime: formatDate(end), } }