帮助中心数据实现动态渲染
This commit is contained in:
21
src/lib/utils/date.ts
Normal file
21
src/lib/utils/date.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
export function formatDate(
|
||||
dateStr?: string | null,
|
||||
format: string = 'YYYY-MM-DD',
|
||||
fallback: string = '-',
|
||||
): string {
|
||||
if (!dateStr) return fallback
|
||||
|
||||
const date = new Date(dateStr)
|
||||
if (isNaN(date.getTime())) return fallback
|
||||
|
||||
const map: Record<string, string | number> = {
|
||||
YYYY: date.getFullYear(),
|
||||
MM: String(date.getMonth() + 1).padStart(2, '0'),
|
||||
DD: String(date.getDate()).padStart(2, '0'),
|
||||
HH: String(date.getHours()).padStart(2, '0'),
|
||||
mm: String(date.getMinutes()).padStart(2, '0'),
|
||||
ss: String(date.getSeconds()).padStart(2, '0'),
|
||||
}
|
||||
|
||||
return format.replace(/YYYY|MM|DD|HH|mm|ss/g, matched => String(map[matched]))
|
||||
}
|
||||
Reference in New Issue
Block a user