18 lines
515 B
JavaScript
18 lines
515 B
JavaScript
|
|
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])
|
||
|
|
}
|