/**
*
* @param {Date/String} date 需格式化的日期
* @param {String} format 格式化 例:‘yyyy-MM-dd hh:mm:ss‘
*/
formatDate = (date, format = ‘yyyy-MM-dd‘) => {
if (!date) return
if (typeof date === ‘number‘) {
date = new Date(date)
}
date = new Date(date)
let d = {
‘M+‘: date.getMonth() + 1,
‘d+‘: date.getDate(),
‘h+‘: date.getHours(),
‘m+‘: date.getMinutes(),
‘s+‘: date.getSeconds(),
‘q+‘: Math.floor((date.getMonth() + 3) / 3),
‘S+‘: date.getMilliseconds()
}
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (date.getFullYear() + ‘‘).substr(4 - RegExp.$1.length))
}
for (let k in d) {
if (new RegExp(‘(‘ + k + ‘)‘).test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? d[k] : (‘00‘ + d[k]).substr((‘‘ + d[k]).length))
}
}
return format
}