diff --git a/common/utils/utils.js b/common/utils/utils.js index 07ae857..a90bbaa 100644 --- a/common/utils/utils.js +++ b/common/utils/utils.js @@ -2,7 +2,7 @@ * @Author: ch * @Date: 2022-03-17 19:15:10 * @LastEditors: ch - * @LastEditTime: 2022-03-22 17:30:38 + * @LastEditTime: 2022-04-24 18:15:27 * @Description: 一些无法归类的公共方法容器 */ @@ -73,7 +73,51 @@ const IsDouble = (str) => { if (result == null) return false return true } +/** + * + * 时间格式化 + * @param {number|string|Date} d 时间参数能被new Date识别的数字,字符串,日期 + * @param {string} fmt 时间格式参数 字符串类型 默认'yyyy/mm/dd' + */ +const formatDate = (date, fmt = 'yyyy/mm/dd' ) =>{ + // 处理不识别的时间表示字符串,如2020年01月01日00时00分00秒 + const dateType = getVarType(date); + if(dateType === 'string'){ + date = date.replace(/\D+/ig,'/'); + let arr = date.split('/'); + // 长度大于3说明带了时分秒信息 特殊时间处理格式 + if(arr.length > 3){ + let time = ` ${arr[3]}:${arr[4]}:${arr[5]}` + arr.length = 3; + date = arr.join('/') + time; + } + }; + try{ + date = date ? dateType === 'date' ? date : new Date(date) : new Date(); + } catch(e){ + throw new Error('不能识别的时间格式'); + } + const o = { + 'm+': date.getMonth() + 1, //月份 + 'd+': date.getDate(), //日 + 'h+': date.getHours(), //小时 + 'i+': date.getMinutes(), //分 + 's+': date.getSeconds(), //秒ji“ + 'q+': Math.floor((date.getMonth() + 3) / 3), //季度 + 'l+': date.getMilliseconds() //毫秒 + }; + if (/(y+)/i.test(fmt)) { + fmt = fmt.replace(RegExp.$1, (date.getFullYear().toString()).substr(4 - RegExp.$1.length)); + } + for (let k in o) { + if (new RegExp(`(${k})`, 'i').test(fmt)) { + const str = o[k].toString(); + fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : (`0${str}`).substr(str.length-1)); + } + } + return fmt; +} // 工具类的文件需要把文件提供的工具类统一放最下方做一个统一输出 export { // async await 标识结果处理 @@ -87,5 +131,7 @@ export { // 判断是否为整数 IsInteger, // 判断是否double或float - IsDouble + IsDouble, + // 时间格式化 + formatDate } \ No newline at end of file diff --git a/components/UiPageHeader.vue b/components/UiPageHeader.vue index 248995c..51befdc 100644 --- a/components/UiPageHeader.vue +++ b/components/UiPageHeader.vue @@ -2,7 +2,7 @@ * @Author: ch * @Date: 2022-03-25 10:11:37 * @LastEditors: ch - * @LastEditTime: 2022-04-22 14:16:14 + * @LastEditTime: 2022-04-24 15:36:57 * @Description: file content -->