支持格式
- YYYY-MM-DD HH:mm:ss
- YYYY-MM-DD HH:mm
- YYYY-MM-DD HH
- YYYY-MM-DD
- YYYY-MM
- YYYY
- YYYY、MM、DD、HH、mm、ss 任意
function dateFormatter(formatter, date) => {
date = (date ? new Date(date) : new Date)
const Y = date.getFullYear() + '',
M = date.getMonth() + 1,
D = date.getDate(),
H = date.getHours(),
m = date.getMinutes(),
s = date.getSeconds()
return formatter.replace(/YYYY|yyyy/g, Y)
.replace(/YY|yy/g, Y.substr(2, 2))
.replace(/MM/g, (M < 10 ? '0' : '') + M)
.replace(/DD/g, (D < 10 ? '0' : '') + D)
.replace(/HH|hh/g, (H < 10 ? '0' : '') + H)
.replace(/mm/g, (m < 10 ? '0' : '') + m)
.replace(/ss/g, (s < 10 ? '0' : '') + s)
}
使用案例
dateFormatter('YYYY-MM-DD HH:mm:ss', new Date())