Files
dukang/apps/mini-user/src/lib/datetime.ts
T
jacy 2088a1ee19 fix(mini-user): 去掉 Intl,兼容微信小程序无 Intl 环境
门店详情走马灯与核销成功时间改为纯 Date 计算东八区格式。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-03 14:37:44 +08:00

17 lines
874 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/** 格式化为 Asia/Shanghai2026-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())}`;
}