- 替换散落的 toISOString().slice(0,10) / split('T')[0] / replace('T',' ')(UTC 语义用 dayjs(...).utc() 保持完全一致)
- Asia/Shanghai 固定时区日期(Intl.DateTimeFormat en-CA)改用 dayjs().utcOffset(8)
- 月份边界 first/last、daysInMonth、nextMonth、shiftDate/addDays 等改 dayjs 简洁实现
- 涉及 attendance/classes/classrooms/room/dashboard/bills/expenses/occupancies/sync/ai-chat/rental 等 28 个文件
17 lines
592 B
TypeScript
17 lines
592 B
TypeScript
import dayjs from '../common/dayjs';
|
|
const DATE_ONLY_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
|
|
const ISO_DATE_PREFIX_PATTERN = /^(\d{4}-\d{2}-\d{2})T/;
|
|
|
|
export function normalizeDateOnly(value?: string | null): string | null | undefined {
|
|
if (value == null || value === '') return value;
|
|
if (DATE_ONLY_PATTERN.test(value)) return value;
|
|
|
|
const isoPrefix = ISO_DATE_PREFIX_PATTERN.exec(value)?.[1];
|
|
if (isoPrefix) {
|
|
const date = new Date(value);
|
|
if (!Number.isNaN(date.getTime())) return dayjs(date).utc().format('YYYY-MM-DD');
|
|
}
|
|
|
|
throw new Error(`无效日期格式: ${value}`);
|
|
}
|