js把时间戳转化为想要的格式
年月日,时分秒 根据需求传
function formatTime(number, format) {
const formateArr = ['Y', 'M', 'D', 'h', 'm', 's']
const returnArr = []
const date = new Date(number)
returnArr.push(date.getFullYear())
returnArr.push(this.formatNumber(date.getMonth() + 1))
returnArr.push(this.formatNumber(date.getDate()))
returnArr.push(this.formatNumber(date.getHours()))
returnArr.push(this.formatNumber(date.getMinutes()))
returnArr.push(this.formatNumber(date.getSeconds()))
for (const i in returnArr) {
format = format.replace(formateArr[i], returnArr[i])
}
return format
}
function formatNumber(n) {
n = n.toString()
return n[1] ? n : `0${n}`
}
formatTime(1629960900000,'Y-M-D h:m') //'2021-08-26 14:55'