防腐
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* 时间格式化工具
|
||||
* 挂载到 uni.formatTime,支持以下格式:
|
||||
* uni.formatTime(date, 'YYYY-MM-DD HH:mm:ss')
|
||||
* uni.formatTime(timestamp, 'YYYY/MM/DD')
|
||||
*/
|
||||
|
||||
const pad = (n) => String(n).padStart(2, '0')
|
||||
|
||||
const formatTime = (date, fmt = 'YYYY-MM-DD HH:mm:ss') => {
|
||||
if (!date) return ''
|
||||
const d = date instanceof Date ? date : new Date(date)
|
||||
if (isNaN(d.getTime())) return ''
|
||||
|
||||
const tokens = {
|
||||
'Y+': d.getFullYear(),
|
||||
'M+': d.getMonth() + 1,
|
||||
'D+': d.getDate(),
|
||||
'H+': d.getHours(),
|
||||
'm+': d.getMinutes(),
|
||||
's+': d.getSeconds(),
|
||||
}
|
||||
|
||||
for (const [key, val] of Object.entries(tokens)) {
|
||||
const match = fmt.match(new RegExp(key))
|
||||
if (match) {
|
||||
const len = match[0].length
|
||||
const str = key === 'Y+' ? String(val).slice(-len) : len === 1 ? String(val) : pad(val)
|
||||
fmt = fmt.replace(match[0], str)
|
||||
}
|
||||
}
|
||||
|
||||
return fmt
|
||||
}
|
||||
|
||||
uni.formatTime = formatTime
|
||||
|
||||
export default formatTime
|
||||
Reference in New Issue
Block a user