first commit
This commit is contained in:
18
utils/date.js
Normal file
18
utils/date.js
Normal file
@@ -0,0 +1,18 @@
|
||||
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])
|
||||
}
|
||||
Reference in New Issue
Block a user