PC端小程序不兼容版本
padStart方法不支持,再PC端会报
export function dateFormat (format, number) {
let ret
let opt = {
'Y+': date.getFullYear().toString(),
'm+': (date.getMonth() + 1).toString(),
'd+': date.getDate().toString(),
'H+': date.getHours().toString(),
'M+': date.getMinutes().toString(),
'S+': date.getSeconds().toString()
}
for (let k in opt) {
ret = new RegExp('(' + k + ')').exec(fmt)
if (ret) {
fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0')))
}
}
return fmt
}
兼容版本
export function dateFormat (format, number) {
var formateArr = ['YY', 'mm', 'dd', 'HH', 'MM', 'SS']
var returnArr = []
var date = new Date(number)
returnArr.push(date.getFullYear())
returnArr.push(formatNumber(date.getMonth() + 1))
returnArr.push(formatNumber(date.getDate()))
returnArr.push(formatNumber(date.getHours()))
returnArr.push(formatNumber(date.getMinutes()))
returnArr.push(formatNumber(date.getSeconds()))
for (var i in returnArr) {
format = format.replace(formateArr[i], returnArr[i])
}
return format
}