【前端】纸掌柜h5端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
830 B

// 日期转换方法
/**
* 日期格式化,样例 yyyy-mm-dd hh:MM:ss
* @param date Date 需要转换的日期
* @param fmt string 转化的格式
*/
const dateTimeFormat = (date, fmt) => {
if (!date) {
throw new Error('日期不正确')
}
let ret
const 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
}
const pubFn = {
dateTimeFormat
}
export default pubFn