时间格式化工具函数封装
按照格式处理返回正确的时间
/**
* 工具函数封装
* @param1 {String}: value待格式时间
* @param2 {String}: scla时间格式 默认 yyyy-MM-dd hh:mm:ss
* @return {type}: returnName
*/
export const formateDate = (value, scla) => {
let date = new Date(value)
let fmt = scla || 'yyyy-MM-dd hh:mm:ss'
if(/(y+)/.test(fmt)){
fmt = fmt.replace(RegExp.$1,date.getFullYear())
}
const o = {
'M+': date.getMonth()+1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
}
for(let k in o ){
if(new RegExp(`(${k})`).test(fmt)){
const val = o[k] + ''
fmt = fmt.replace(RegExp.$1,RegExp.$1.length == 1? val: ('00'+val).substr(val.length))
}
}
return fmt
}
调用
val = '2022-05-27T06:36:25.982Z'
return formateDate(val) // 2022-05-27 14:36:25
return formateDate(val, 'yyyy年MM月dd日') // 2022年05月27日

被折叠的 条评论
为什么被折叠?



