2088a1ee19
门店详情走马灯与核销成功时间改为纯 Date 计算东八区格式。 Co-authored-by: Cursor <cursoragent@cursor.com>
17 lines
874 B
TypeScript
17 lines
874 B
TypeScript
/** 格式化为 Asia/Shanghai:2026-08-03 15:14:30(不依赖 Intl,兼容微信小程序) */
|
||
export function formatShanghaiDateTime(input?: string | Date | null): string {
|
||
if (input == null || input === '') return '—';
|
||
if (typeof input === 'string') {
|
||
const s = input.trim();
|
||
if (/^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}/.test(s)) {
|
||
return s.slice(0, 19).replace('T', ' ');
|
||
}
|
||
}
|
||
const d = input instanceof Date ? input : new Date(input);
|
||
if (Number.isNaN(d.getTime())) return '—';
|
||
// 用 UTC 读数 + 固定东八区偏移,避免依赖 Intl / 设备时区 API 差异
|
||
const sh = new Date(d.getTime() + 8 * 60 * 60 * 1000);
|
||
const p = (n: number) => String(n).padStart(2, '0');
|
||
return `${sh.getUTCFullYear()}-${p(sh.getUTCMonth() + 1)}-${p(sh.getUTCDate())} ${p(sh.getUTCHours())}:${p(sh.getUTCMinutes())}:${p(sh.getUTCSeconds())}`;
|
||
}
|