M6: 创建 utils/date.ts 统一日期工具函数(formatDate/formatDateTime/toRelativeDate 等) M8: 28 个 SCSS 文件 font-size 20px → 22px 全量适老化 M7: request.ts 增加 403 权限不足/5xx 服务器错误/网络超时异常统一拦截
75 lines
2.4 KiB
TypeScript
75 lines
2.4 KiB
TypeScript
/**
|
|
* 统一日期工具函数 — 替代分散在各页面的内联日期格式化
|
|
*/
|
|
|
|
/** 格式化为 YYYY-MM-DD */
|
|
export function formatDate(iso: string | null | undefined): string {
|
|
if (!iso) return '-';
|
|
const d = new Date(iso);
|
|
if (isNaN(d.getTime())) return '-';
|
|
const y = d.getFullYear();
|
|
const m = String(d.getMonth() + 1).padStart(2, '0');
|
|
const day = String(d.getDate()).padStart(2, '0');
|
|
return `${y}-${m}-${day}`;
|
|
}
|
|
|
|
/** 格式化为 YYYY-MM-DD HH:mm */
|
|
export function formatDateTime(iso: string | null | undefined): string {
|
|
if (!iso) return '-';
|
|
const d = new Date(iso);
|
|
if (isNaN(d.getTime())) return '-';
|
|
const date = formatDate(iso);
|
|
const h = String(d.getHours()).padStart(2, '0');
|
|
const min = String(d.getMinutes()).padStart(2, '0');
|
|
return `${date} ${h}:${min}`;
|
|
}
|
|
|
|
/** 格式化为 YYYY-MM-DD HH:mm:ss */
|
|
export function formatDateTimeFull(iso: string | null | undefined): string {
|
|
if (!iso) return '-';
|
|
const d = new Date(iso);
|
|
if (isNaN(d.getTime())) return '-';
|
|
const base = formatDateTime(iso);
|
|
const s = String(d.getSeconds()).padStart(2, '0');
|
|
return `${base}:${s}`;
|
|
}
|
|
|
|
/** 获取今天的 YYYY-MM-DD */
|
|
export function today(): string {
|
|
const d = new Date();
|
|
const y = d.getFullYear();
|
|
const m = String(d.getMonth() + 1).padStart(2, '0');
|
|
const day = String(d.getDate()).padStart(2, '0');
|
|
return `${y}-${m}-${day}`;
|
|
}
|
|
|
|
/** 相对时间:今天/昨天/前天/更早 */
|
|
export function toRelativeDate(iso: string | null | undefined): string {
|
|
if (!iso) return '-';
|
|
const d = new Date(iso);
|
|
if (isNaN(d.getTime())) return '-';
|
|
|
|
const now = new Date();
|
|
const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate());
|
|
const targetStart = new Date(d.getFullYear(), d.getMonth(), d.getDate());
|
|
const diffDays = Math.round((todayStart.getTime() - targetStart.getTime()) / 86400000);
|
|
|
|
if (diffDays === 0) return '今天';
|
|
if (diffDays === 1) return '昨天';
|
|
if (diffDays === 2) return '前天';
|
|
return formatDate(iso);
|
|
}
|
|
|
|
/** 格式化为中文月日 + 星期 */
|
|
export function formatMonthDayWeek(iso: string | null | undefined): string {
|
|
if (!iso) return '';
|
|
const d = new Date(iso);
|
|
if (isNaN(d.getTime())) return '';
|
|
|
|
const weekdays = ['日', '一', '二', '三', '四', '五', '六'];
|
|
const m = d.getMonth() + 1;
|
|
const day = d.getDate();
|
|
const w = weekdays[d.getDay()];
|
|
return `${m}月${day}日 周${w}`;
|
|
}
|