Files
koc-loop/koc-portal/app/date-utils.ts

27 lines
767 B
TypeScript
Raw Normal View History

2026-07-30 12:06:41 +08:00
export function parseStoredDate(value: string) {
const trimmed = value.trim();
const normalized = /^\d{4}-\d{2}-\d{2}$/.test(trimmed)
? `${trimmed}T00:00:00+08:00`
: /^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?$/.test(
trimmed,
)
? `${trimmed.replace(" ", "T")}Z`
: trimmed;
return new Date(normalized);
}
export function formatShanghaiDate(
value?: string | null,
withTime = false,
) {
if (!value) return "—";
const date = parseStoredDate(value);
if (Number.isNaN(date.getTime())) return value;
return new Intl.DateTimeFormat("zh-CN", {
timeZone: "Asia/Shanghai",
month: "2-digit",
day: "2-digit",
...(withTime ? { hour: "2-digit", minute: "2-digit" } : {}),
}).format(date);
}