formatTime.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import dayjs from 'dayjs'
  2. /**
  3. * 时间日期转换
  4. * @param date 当前时间,new Date() 格式
  5. * @param format 需要转换的时间格式字符串
  6. * @description format 字符串随意,如 `YYYY-mm、YYYY-mm-dd`
  7. * @description format 季度:"YYYY-mm-dd HH:MM:SS QQQQ"
  8. * @description format 星期:"YYYY-mm-dd HH:MM:SS WWW"
  9. * @description format 几周:"YYYY-mm-dd HH:MM:SS ZZZ"
  10. * @description format 季度 + 星期 + 几周:"YYYY-mm-dd HH:MM:SS WWW QQQQ ZZZ"
  11. * @returns 返回拼接后的时间字符串
  12. */
  13. export function formatDate(date: Date, format: string): string {
  14. const we = date.getDay() // 星期
  15. const z = getWeek(date) // 周
  16. const qut = Math.floor((date.getMonth() + 3) / 3).toString() // 季度
  17. const opt: { [key: string]: string } = {
  18. 'Y+': date.getFullYear().toString(), // 年
  19. 'm+': (date.getMonth() + 1).toString(), // 月(月份从0开始,要+1)
  20. 'd+': date.getDate().toString(), // 日
  21. 'H+': date.getHours().toString(), // 时
  22. 'M+': date.getMinutes().toString(), // 分
  23. 'S+': date.getSeconds().toString(), // 秒
  24. 'q+': qut // 季度
  25. }
  26. // 中文数字 (星期)
  27. const week: { [key: string]: string } = {
  28. '0': '日',
  29. '1': '一',
  30. '2': '二',
  31. '3': '三',
  32. '4': '四',
  33. '5': '五',
  34. '6': '六'
  35. }
  36. // 中文数字(季度)
  37. const quarter: { [key: string]: string } = {
  38. '1': '一',
  39. '2': '二',
  40. '3': '三',
  41. '4': '四'
  42. }
  43. if (/(W+)/.test(format))
  44. format = format.replace(
  45. RegExp.$1,
  46. RegExp.$1.length > 1 ? (RegExp.$1.length > 2 ? '星期' + week[we] : '周' + week[we]) : week[we]
  47. )
  48. if (/(Q+)/.test(format))
  49. format = format.replace(
  50. RegExp.$1,
  51. RegExp.$1.length == 4 ? '第' + quarter[qut] + '季度' : quarter[qut]
  52. )
  53. if (/(Z+)/.test(format))
  54. format = format.replace(RegExp.$1, RegExp.$1.length == 3 ? '第' + z + '周' : z + '')
  55. for (const k in opt) {
  56. const r = new RegExp('(' + k + ')').exec(format)
  57. // 若输入的长度不为1,则前面补零
  58. if (r)
  59. format = format.replace(
  60. r[1],
  61. RegExp.$1.length == 1 ? opt[k] : opt[k].padStart(RegExp.$1.length, '0')
  62. )
  63. }
  64. return format
  65. }
  66. // 日期格式化
  67. export function parseTime(time: any, pattern?: string) {
  68. if (arguments.length === 0 || !time) {
  69. return null
  70. }
  71. const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  72. let date
  73. if (typeof time === 'object') {
  74. date = time
  75. } else {
  76. if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
  77. time = parseInt(time)
  78. } else if (typeof time === 'string') {
  79. time = time
  80. .replace(new RegExp(/-/gm), '/')
  81. .replace('T', ' ')
  82. .replace(new RegExp(/\.\d{3}/gm), '')
  83. }
  84. if (typeof time === 'number' && time.toString().length === 10) {
  85. time = time * 1000
  86. }
  87. date = new Date(time)
  88. }
  89. const formatObj = {
  90. y: date.getFullYear(),
  91. m: date.getMonth() + 1,
  92. d: date.getDate(),
  93. h: date.getHours(),
  94. i: date.getMinutes(),
  95. s: date.getSeconds(),
  96. a: date.getDay()
  97. }
  98. const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
  99. let value = formatObj[key]
  100. // Note: getDay() returns 0 on Sunday
  101. if (key === 'a') {
  102. return ['日', '一', '二', '三', '四', '五', '六'][value]
  103. }
  104. if (result.length > 0 && value < 10) {
  105. value = '0' + value
  106. }
  107. return value || 0
  108. })
  109. return time_str
  110. }
  111. /**
  112. * 获取当前日期是第几周
  113. * @param dateTime 当前传入的日期值
  114. * @returns 返回第几周数字值
  115. */
  116. export function getWeek(dateTime: Date): number {
  117. const temptTime = new Date(dateTime.getTime())
  118. // 周几
  119. const weekday = temptTime.getDay() || 7
  120. // 周1+5天=周六
  121. temptTime.setDate(temptTime.getDate() - weekday + 1 + 5)
  122. let firstDay = new Date(temptTime.getFullYear(), 0, 1)
  123. const dayOfWeek = firstDay.getDay()
  124. let spendDay = 1
  125. if (dayOfWeek != 0) spendDay = 7 - dayOfWeek + 1
  126. firstDay = new Date(temptTime.getFullYear(), 0, 1 + spendDay)
  127. const d = Math.ceil((temptTime.valueOf() - firstDay.valueOf()) / 86400000)
  128. const result = Math.ceil(d / 7)
  129. return result
  130. }
  131. /**
  132. * 将时间转换为 `几秒前`、`几分钟前`、`几小时前`、`几天前`
  133. * @param param 当前时间,new Date() 格式或者字符串时间格式
  134. * @param format 需要转换的时间格式字符串
  135. * @description param 10秒: 10 * 1000
  136. * @description param 1分: 60 * 1000
  137. * @description param 1小时: 60 * 60 * 1000
  138. * @description param 24小时:60 * 60 * 24 * 1000
  139. * @description param 3天: 60 * 60* 24 * 1000 * 3
  140. * @returns 返回拼接后的时间字符串
  141. */
  142. export function formatPast(param: string | Date, format = 'YYYY-mm-dd HH:MM:SS'): string {
  143. // 传入格式处理、存储转换值
  144. let t: any, s: number
  145. // 获取js 时间戳
  146. let time: number = new Date().getTime()
  147. // 是否是对象
  148. typeof param === 'string' || 'object' ? (t = new Date(param).getTime()) : (t = param)
  149. // 当前时间戳 - 传入时间戳
  150. time = Number.parseInt(`${time - t}`)
  151. if (time < 10000) {
  152. // 10秒内
  153. return '刚刚'
  154. } else if (time < 60000 && time >= 10000) {
  155. // 超过10秒少于1分钟内
  156. s = Math.floor(time / 1000)
  157. return `${s}秒前`
  158. } else if (time < 3600000 && time >= 60000) {
  159. // 超过1分钟少于1小时
  160. s = Math.floor(time / 60000)
  161. return `${s}分钟前`
  162. } else if (time < 86400000 && time >= 3600000) {
  163. // 超过1小时少于24小时
  164. s = Math.floor(time / 3600000)
  165. return `${s}小时前`
  166. } else if (time < 259200000 && time >= 86400000) {
  167. // 超过1天少于3天内
  168. s = Math.floor(time / 86400000)
  169. return `${s}天前`
  170. } else {
  171. // 超过3天
  172. const date = typeof param === 'string' || 'object' ? new Date(param) : param
  173. return formatDate(date, format)
  174. }
  175. }
  176. /**
  177. * 时间问候语
  178. * @param param 当前时间,new Date() 格式
  179. * @description param 调用 `formatAxis(new Date())` 输出 `上午好`
  180. * @returns 返回拼接后的时间字符串
  181. */
  182. export function formatAxis(param: Date): string {
  183. const hour: number = new Date(param).getHours()
  184. if (hour < 6) return '凌晨好'
  185. else if (hour < 9) return '早上好'
  186. else if (hour < 12) return '上午好'
  187. else if (hour < 14) return '中午好'
  188. else if (hour < 17) return '下午好'
  189. else if (hour < 19) return '傍晚好'
  190. else if (hour < 22) return '晚上好'
  191. else return '夜里好'
  192. }
  193. /**
  194. * 将毫秒,转换成时间字符串。例如说,xx 分钟
  195. *
  196. * @param ms 毫秒
  197. * @returns {string} 字符串
  198. */
  199. export function formatPast2(ms) {
  200. const day = Math.floor(ms / (24 * 60 * 60 * 1000))
  201. const hour = Math.floor(ms / (60 * 60 * 1000) - day * 24)
  202. const minute = Math.floor(ms / (60 * 1000) - day * 24 * 60 - hour * 60)
  203. const second = Math.floor(ms / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60)
  204. if (day > 0) {
  205. return day + '天' + hour + '小时' + minute + '分钟'
  206. }
  207. if (hour > 0) {
  208. return hour + '小时' + minute + '分钟'
  209. }
  210. if (minute > 0) {
  211. return minute + '分钟'
  212. }
  213. if (second > 0) {
  214. return second + '秒'
  215. } else {
  216. return 0 + '秒'
  217. }
  218. }
  219. /**
  220. * element plus 的时间 Formatter 实现,使用 YYYY-MM-DD HH:mm:ss 格式
  221. *
  222. * @param row 行数据
  223. * @param column 字段
  224. * @param cellValue 字段值
  225. */
  226. // @ts-ignore
  227. export const dateFormatter = (row, column, cellValue) => {
  228. if (!cellValue) {
  229. return
  230. }
  231. return dayjs(cellValue).format('YYYY-MM-DD HH:mm:ss')
  232. }