优化时间展示

This commit is contained in:
yml
2026-05-23 01:56:16 +08:00
parent c9db34ddda
commit 744f572f6c
17 changed files with 85 additions and 24 deletions
+23
View File
@@ -0,0 +1,23 @@
type DateInput = string | number | Date | null | undefined
export function formatDateTime(value: DateInput, fallback = '-') {
if (!value) return fallback
const date = value instanceof Date ? value : new Date(value)
if (Number.isNaN(date.getTime())) {
return typeof value === 'string' && value.trim() ? value.replace('T', ' ') : fallback
}
return [
date.getFullYear(),
pad(date.getMonth() + 1),
pad(date.getDate()),
].join('-') + ` ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`
}
export function formatDateMinute(value: DateInput, fallback = '-') {
const formatted = formatDateTime(value, fallback)
return formatted === fallback ? fallback : formatted.slice(0, 16)
}
function pad(value: number) {
return String(value).padStart(2, '0')
}