// 把时间转换成自己想要的格式 export const dateFormat = (t, str) => { // 处理t的默认值 if (!t) t = new Date() // 强制转换类型, 防止用户输入的不是时间 t = t + ‘‘ // 处理ios的兼容性 if (t.includes(‘-‘)) { t = t.replace(/-/g, ‘/‘) } // 以地图的形式获取 时间 var map = { yyyy: new Date(t).getFullYear(), MM: (new Date(t).getMonth() + 1 + ‘‘).padStart(2, ‘0‘), dd: (new Date(t).getDate() + ‘‘).padStart(2, ‘0‘), hh: (new Date(t).getHours() + ‘‘).padStart(2, ‘0‘), mm: (new Date(t).getMinutes() + ‘‘).padStart(2, ‘0‘), ss: (new Date(t).getSeconds() + ‘‘).padStart(2, ‘0‘) } console.log(‘传入的格式为‘ + str) // 判断用户是否输入了时间格式 if (!str || !str.length) { return `${map.yyyy}-${map.MM}-${map.dd} ${map.hh}:${map.mm}:${map.ss}` } // 用户输入了 时间格式 let time = str // 判断是否包含年份 if (time.includes(‘yyyy‘)) { time = time.replace(‘yyyy‘, map.yyyy) } // 判断是否包含月份 if (time.includes(‘MM‘)) { time = time.replace(‘MM‘, map.MM) } // 判断是否包含日期 if (time.includes(‘dd‘)) { time = time.replace(‘dd‘, map.dd) } // 判断是否包含小时 if (time.includes(‘hh‘)) { time = time.replace(‘hh‘, map.hh) } // 判断是否包含日期 if (time.includes(‘mm‘)) { time = time.replace(‘mm‘, map.mm) } // 判断是否包含日期 if (time.includes(‘ss‘)) { time = time.replace(‘ss‘, map.ss) } return time } // 获取使用时长 export const getUsingTime = (timeStart, timeEnd, timeStr) => { // 防止没有输入时间,从而引起报错 if (!timeStart || !timeEnd) return ‘--:--:--‘ // 强行转换格式 timeStart = timeStart + ‘‘ timeEnd = timeEnd + ‘‘ // 解决IOS兼容性问题 if (timeStart.includes(‘-‘)) { timeStart = timeStart.replace(/-/g, ‘/‘) } if (timeEnd.includes(‘-‘)) { timeEnd = timeEnd.replace(/-/g, ‘/‘) } const tem = new Date(timeEnd).getTime() - new Date(timeStart).getTime() // 判度时间是否小于零 if (tem <= 0) return ‘00:00:00‘ const map = { hh: (parseInt(tem / 1000 / 60 / 60) + ‘‘).padStart(2, ‘0‘), mm: (parseInt((tem / 1000 / 60) % 60) + ‘‘).padStart(2, ‘0‘), ss: (parseInt((tem / 1000) % 60) + ‘‘).padStart(2, ‘0‘), MM: (parseInt(tem / 1000 / 60) + ‘‘).padStart(2, ‘0‘) } // 根据用户传递的字符串模板, 返回对应的时间 // 用户没有传递时间格式, 返回默认格式 if (!timeStr) { return `${map.hh}:${map.mm}:${map.ss}` } // 传递了格式, 但是没有包含小时, 把小时合并进分钟数中 let str = timeStr if (!str.includes(‘hh‘)) { if (str.includes(‘mm‘)) { str = str.replace(‘mm‘, map.MM) } if (str.includes(‘ss‘)) { str = str.replace(‘ss‘, map.ss) } return str } // 否则根据用户传递的时分秒, 进行全部替换 if (str.includes(‘hh‘)) { str = str.replace(‘hh‘, map.hh) } if (str.includes(‘mm‘)) { str = str.replace(‘mm‘, map.mm) } if (str.includes(‘ss‘)) { str = str.replace(‘ss‘, map.ss) } return str } // 获取指定的日期 export const getAppointDate = (day, timeStr) => { // 如果没有传递时间或者传递的时间为false或者null或者 ‘‘, 默认获取今天林晨的时间 // 1. 没传天数, 且 没传需要的时间格式, 返回 今天的 默认的时间格式 if (!day && !timeStr) { const yyyy = new Date().getFullYear() const mm = (new Date().getMonth() + 1 + ‘‘).padStart(2, ‘0‘) const dd = (new Date().getDate() + ‘‘).padStart(2, ‘0‘) return `${yyyy}-${mm}-${dd} 00:00:00` } // 天数传递的boolean值为false, 但是传递了时间格式 if (!day && timeStr) { let str = timeStr const yyyy = new Date().getFullYear() const mm = (new Date().getMinutes() + 1 + ‘‘).padStart(2, ‘0‘) const dd = (new Date().getDate() + ‘‘).padStart(2, ‘0‘) if (str.includes(‘yyyy‘)) { str = str.replace(‘yyyy‘, yyyy) } if (str.includes(‘mm‘)) { str = str.replace(‘mm‘, mm) } if (str.includes(‘dd‘)) { str = str.replace(‘dd‘, dd) } return str } // 传大于零的天数 但是没传时间字符 const tem = new Date().getTime() + day * 24 * 60 * 60 * 1000 const t = new Date(tem) const yyyy = t.getFullYear() const mm = (t.getMonth() + 1 + ‘‘).padStart(2, ‘0‘) const dd = (t.getDate() + ‘‘).padStart(2, ‘0‘) if (!timeStr) { return `${yyyy}-${mm}-${dd} 00:00:00` } else { let str = timeStr if (str.includes(‘yyyy‘)) { str = str.replace(‘yyyy‘, yyyy) } if (str.includes(‘mm‘)) { str = str.replace(‘mm‘, mm) } if (str.includes(‘dd‘)) { str = str.replace(‘dd‘, dd) } return str } }
下面的一个说明文档 用md 写的, 浏览器看着会很乱
### 最近发现时间格式化用得有点频繁, 所以自己写了一个时间格式化的库, 方便工作中使用 兼容性方面, 兼容了 IOS 系统 对于 2020-12-00 12:00:00 这种格式的时间无法正常解析的问题 但是很多 UI 返回的时间, 依旧需要进行手动处理, 比如前段时间我用 mintui 的时间选择器 picker , 返回的时间格式, 在 Android 上面正常解析为时间, 但是在 IOS 上面并不行, 不一定能是-的问题, 也有可能是你传入的时间,别人 IOS 根本无法使用时间函数 _使用方式_ ```js 1. 在 vue 中使用 import 导入 import { dateFormat } from ‘../utils/dateFormat.js‘ 2. 在需要处理的地方调用 ``` #### 函数 dateFormat 用于获取指定格式的时间 ##### 1. 不传入任何参数, 直接调用 默认获取当前的时间, 格式为 `2020-10-04 16:30:39` ```js // 调用 const res = dateFormat() console.log(res) // 返回结果 2020-10-04 16:30:39 ``` ##### 2. 传入时间, 不传递格式 如果没有携带时分秒, 获取的为, 传递的日期的 零时零分零秒时间, 格式为 `2020-12-20 00:00:00` ```js // 调用 const res = dateFormat(‘2020-12-20‘) console.log(res, ‘res‘) // 返回结果 2020-12-20 00:00:00 ``` ##### 3. 传入时间, 并且携带需要的时间格式 ###### 参数说明 yyyy ======== 年份 MM ========= 月份(已经加 1) dd ========== 日 hh ========== 小时 mm ========== 分钟 ss ========== 秒 ###### 说明: 参数传递根据自己的需求进行组合 ```js 例如我需要 2020年12月24日中午十二点的时间字符, 如下 24-12-2020 12:00:00 只需要传递 ‘dd-MM-yyyy hh:mm:ss‘ 再例如我需要汉字的组成的 2020年12分20号 只需要传递 ‘yyyy年MM月dd号‘ ``` ```js // 调用 const res = dateFormat(‘2020-12-20‘, ‘yyyy-MM-dd hh:mm:ss‘) console.log(res) // 得到的结果为 2020-12-20 00:00:00 // 调用 const res = dateFormat(‘2020-12-20‘, ‘dd-MM-yyyy hh:mm:ss‘) console.log(‘返回的格式为‘ + res) // 打印得到的结果 返回的格式为20-12-2020 00:00:00 // 调用 const res = dateFormat(‘2020-12-20‘, ‘yyyy年MM月dd号‘) console.log(res) // 得到的结果为 2020年12月20号 ``` #### 函数 getUsingTime 获取用户使用的时长 ##### 参数(timeStart,timeEnd,timeStr) timeStart: 开始使用的时间(必填参数) timeEnd: 结束使用的时间(必填参数, 没有传 null 或者 false 或着‘‘) timeStr: 默认 ‘hh:mm:ss‘, 可选参数 ‘hh:mm‘ 或者 ‘mm:ss‘ 可以根据自己需求进行替换, 例如 传入 ‘hh 小时 mm 分钟‘, ‘mm 分钟 ss 秒‘ **注意** 只传分和秒的时候, 会默认把小时计算进分钟数中 ```js // 调用 const res = getUsingTime(‘2020-10-04 12:00:00‘, ‘2020-10-04 13:45:56‘) console.log(res) // 打印结果 01:45:56 // 调用 const res = getUsingTime( ‘2020-10-04 12:00:00‘, ‘2020-10-04 13:45:56‘, ‘mm分钟ss秒‘ ) console.log(res) // 打印结果 105分钟56秒 // 调用 const res = getUsingTime( ‘2020-10-04 12:00:00‘, ‘2020-10-04 13:45:56‘, ‘hh:mm:ss‘ ) console.log(res) // 打印结果 01:45:56 // 调用 const res = getUsingTime( ‘2020-10-04 12:00:00‘, ‘2020-10-04 13:45:56‘, ‘hh小时mm分‘ ) console.log(res) // 打印结果 01小时45分 ``` #### 函数 getAppointDate(day, ‘yyyy-mm-dd 00:00:00‘) 用于获取指定天数之前或者之后或者今天的,默认格式的时间或者指定格式的时间 **注意** 该函数不会处理时分秒, 默认返回的都是当天凌晨的 时间 参数 day: 等于 0 获取今天凌晨的时间, 大于 0, 获取几天后的凌晨时间, 小于零, 获取几天之前的时间 ‘yyyy-mm-dd 00:00:00‘: 需要的时间格式, 只会替换 yyyy 和 mm 和 dd, 后面根据自己需求加后缀 ```js // 调用 const ret = getAppointDate() console.log(ret) // 打印结果 2020-10-05 00:00:00 // 调用 // 当前时间 2020/10/05 // 获取两天后的时间 const ret = getAppointDate(2) console.log(ret) // 打印结果 2020-10-07 00:00:00 // 调用 获取七天之前的日期 const ret = getAppointDate(-7) console.log(ret) // 得到 2020-09-28 00:00:00 // 调用 传递格式 const ret = getAppointDate(-7, ‘yyyy-mm-dd‘) console.log(ret) // 打印得到的结果 2020-09-28 ```
原文:https://www.cnblogs.com/liuyuexue520/p/13770444.html