20 lines
419 B
TypeScript
20 lines
419 B
TypeScript
export function formatLocalDateTime(value: string | null | undefined, fallback = "无") {
|
|
if (!value) {
|
|
return fallback;
|
|
}
|
|
|
|
const date = new Date(value);
|
|
|
|
if (Number.isNaN(date.getTime())) {
|
|
return fallback;
|
|
}
|
|
|
|
return new Intl.DateTimeFormat("zh-CN", {
|
|
year: "numeric",
|
|
month: "2-digit",
|
|
day: "2-digit",
|
|
hour: "2-digit",
|
|
minute: "2-digit",
|
|
hour12: false,
|
|
}).format(date);
|
|
} |