Files
lotteryFront/src/lib/format-local-datetime.ts

27 lines
778 B
TypeScript

function pad2(n: number): string {
return String(n).padStart(2, "0");
}
/**
* 将接口 ISO 8601 时间格式化为浏览器 **本地时区** 下的 `YYYY-MM-DD HH:mm:ss`。
*
* 避免对原始字符串仅 `slice(0,19)`(易把 UTC 刻度误当本地钟面)。
*/
export function formatLocalDateTime(iso: string | null | undefined): string {
if (iso == null || iso === "") {
return "—";
}
const ms = Date.parse(iso);
if (Number.isNaN(ms)) {
return "—";
}
const date = new Date(ms);
const y = date.getFullYear();
const m = pad2(date.getMonth() + 1);
const d = pad2(date.getDate());
const h = pad2(date.getHours());
const min = pad2(date.getMinutes());
const s = pad2(date.getSeconds());
return `${y}-${m}-${d} ${h}:${min}:${s}`;
}