Files

18 lines
515 B
JavaScript
Raw Permalink Normal View History

2026-01-09 11:24:40 +08:00
export function formatDate(isoString, format = 'YYYY-MM-DD HH:mm:ss') {
if (!isoString) return ''
const date = new Date(isoString)
if (isNaN(date.getTime())) return ''
const pad = (n) => n.toString().padStart(2, '0')
const map = {
YYYY: date.getFullYear().toString(),
MM: pad(date.getMonth() + 1),
DD: pad(date.getDate()),
HH: pad(date.getHours()),
mm: pad(date.getMinutes()),
ss: pad(date.getSeconds())
}
return format.replace(/YYYY|MM|DD|HH|mm|ss/g, token => map[token])
}