// 格式化时间,参数可选。默认格式:yyyy-MM-dd hh:mm:ss
export function formatDate(fmt, time) {
let date = new Date()
if (time !== undefined) {
date = new Date(time)
}
fmt = fmt || 'yyyy-MM-dd hh:mm:ss'
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
}
const o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
}
for (const k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
const str = o[k] + ''
fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : ('00' + str).substr(str.length))
}
}
return fmt
}
在所需要的页面引入就好
注意: 函数的第一个参数是你要的时间格式,第二个是你要格式化的时间