时间戳格式化
const formatDate = function (msec, fmt = 'yyyy-MM-dd hh:mm:ss') {
if (typeof msec !== 'number') {
return msec
}
const date = new Date(parseInt(msec))
const opt = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
}
for (let i in opt) {
const reg = new RegExp(`(${i})`)
if (reg.test(fmt)) {
fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? opt[i] : opt[i] < 10 ? '0' + opt[i] : opt[i])
}
}
return fmt
}
** 各种时间格式转换时间戳**
const toTimeStamp = function (str) {
if (typeof str !== 'string') {
return str
}
if (str.length < 6) {
return str
}
let arr = str.split(/[^\d]+/g)
if (arr.length === 1) {
arr = [str.substr(0, 4), str.substr(4, 2), str.substr(6, 2)]
}
return new Date(arr[0], arr[1] - 1, arr[2] || '1', arr[3] || '0', arr[4] || '0', arr[5] || '0').getTime()
}