/** * 日期工具 * * 原始 Dart: lib/utils/date_utils.dart(app_date_utils.DateUtils) * * 业务侧用 `formatDate(date)` 生成聊天时间戳,`todayString()` 用于每日配额 key。 */ /** 格式:`HH:mm`(与 Dart 端 `formatDate` 一致) */ export function formatDate(date: Date = new Date()): string { const hh = String(date.getHours()).padStart(2, "0"); const mm = String(date.getMinutes()).padStart(2, "0"); return `${hh}:${mm}`; } /** 格式:`YYYY-MM-DD`,用于每日配额 key。 */ export function todayString(date: Date = new Date()): string { const y = date.getFullYear(); const m = String(date.getMonth() + 1).padStart(2, "0"); const d = String(date.getDate()).padStart(2, "0"); return `${y}-${m}-${d}`; } /** 格式:人类可读的日期(`MMM D, YYYY`)。 */ export function formatHumanDate(date: Date = new Date()): string { return date.toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric", }); } /** 同一日期(忽略时分秒) */ export function isSameDay(a: Date, b: Date): boolean { return ( a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate() ); }