const dat = 1682238945000
function formatDateTime(time, format) {
const t = new Date(time)
const tf = function(i) {
return (i < 10 ? '0' : '') + i
}
return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a) {
switch (a) {
case 'yyyy':
return tf(t.getFullYear())
case 'MM':
return tf(t.getMonth() + 1)
case 'mm':
return tf(t.getMinutes())
case 'dd':
return tf(t.getDate())
case 'HH':
return tf(t.getHours())
case 'ss':
return tf(t.getSeconds())
}
})
}
formatDateTime(dat,'yyyy-MM-dd HH:mm:ss')
'2023-04-23 16:35:45'
formatDateTime(dat,'yyyy-MM-dd')
'2023-04-23'
支持两种参数
02-22
351
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-30