formatTime.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. return dayjs(date).format(format)
  15. }
  16. // 日期格式化
  17. export function parseTime(time: any, pattern?: string) {
  18. if (arguments.length === 0 || !time) {
  19. return null
  20. }
  21. const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  22. let date
  23. if (typeof time === 'object') {
  24. date = time
  25. } else {
  26. if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
  27. time = parseInt(time)
  28. } else if (typeof time === 'string') {
  29. time = time
  30. .replace(new RegExp(/-/gm), '/')
  31. .replace('T', ' ')
  32. .replace(new RegExp(/\.\d{3}/gm), '')
  33. }
  34. if (typeof time === 'number' && time.toString().length === 10) {
  35. time = time * 1000
  36. }
  37. date = new Date(time)
  38. }
  39. const formatObj = {
  40. y: date.getFullYear(),
  41. m: date.getMonth() + 1,
  42. d: date.getDate(),
  43. h: date.getHours(),
  44. i: date.getMinutes(),
  45. s: date.getSeconds(),
  46. a: date.getDay()
  47. }
  48. const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
  49. let value = formatObj[key]
  50. // Note: getDay() returns 0 on Sunday
  51. if (key === 'a') {
  52. return ['日', '一', '二', '三', '四', '五', '六'][value]
  53. }
  54. if (result.length > 0 && value < 10) {
  55. value = '0' + value
  56. }
  57. return value || 0
  58. })
  59. return time_str
  60. }
  61. /**
  62. * 获取当前日期是第几周
  63. * @param dateTime 当前传入的日期值
  64. * @returns 返回第几周数字值
  65. */
  66. export function getWeek(dateTime: Date): number {
  67. const temptTime = new Date(dateTime.getTime())
  68. // 周几
  69. const weekday = temptTime.getDay() || 7
  70. // 周1+5天=周六
  71. temptTime.setDate(temptTime.getDate() - weekday + 1 + 5)
  72. let firstDay = new Date(temptTime.getFullYear(), 0, 1)
  73. const dayOfWeek = firstDay.getDay()
  74. let spendDay = 1
  75. if (dayOfWeek != 0) spendDay = 7 - dayOfWeek + 1
  76. firstDay = new Date(temptTime.getFullYear(), 0, 1 + spendDay)
  77. const d = Math.ceil((temptTime.valueOf() - firstDay.valueOf()) / 86400000)
  78. const result = Math.ceil(d / 7)
  79. return result
  80. }
  81. /**
  82. * 将时间转换为 `几秒前`、`几分钟前`、`几小时前`、`几天前`
  83. * @param param 当前时间,new Date() 格式或者字符串时间格式
  84. * @param format 需要转换的时间格式字符串
  85. * @description param 10秒: 10 * 1000
  86. * @description param 1分: 60 * 1000
  87. * @description param 1小时: 60 * 60 * 1000
  88. * @description param 24小时:60 * 60 * 24 * 1000
  89. * @description param 3天: 60 * 60* 24 * 1000 * 3
  90. * @returns 返回拼接后的时间字符串
  91. */
  92. export function formatPast(param: string | Date, format = 'YYYY-mm-dd HH:MM:SS'): string {
  93. // 传入格式处理、存储转换值
  94. let t: any, s: number
  95. // 获取js 时间戳
  96. let time: number = new Date().getTime()
  97. // 是否是对象
  98. typeof param === 'string' || 'object' ? (t = new Date(param).getTime()) : (t = param)
  99. // 当前时间戳 - 传入时间戳
  100. time = Number.parseInt(`${time - t}`)
  101. if (time < 10000) {
  102. // 10秒内
  103. return '刚刚'
  104. } else if (time < 60000 && time >= 10000) {
  105. // 超过10秒少于1分钟内
  106. s = Math.floor(time / 1000)
  107. return `${s}秒前`
  108. } else if (time < 3600000 && time >= 60000) {
  109. // 超过1分钟少于1小时
  110. s = Math.floor(time / 60000)
  111. return `${s}分钟前`
  112. } else if (time < 86400000 && time >= 3600000) {
  113. // 超过1小时少于24小时
  114. s = Math.floor(time / 3600000)
  115. return `${s}小时前`
  116. } else if (time < 259200000 && time >= 86400000) {
  117. // 超过1天少于3天内
  118. s = Math.floor(time / 86400000)
  119. return `${s}天前`
  120. } else {
  121. // 超过3天
  122. const date = typeof param === 'string' || 'object' ? new Date(param) : param
  123. return formatDate(date, format)
  124. }
  125. }
  126. /**
  127. * 时间问候语
  128. * @param param 当前时间,new Date() 格式
  129. * @description param 调用 `formatAxis(new Date())` 输出 `上午好`
  130. * @returns 返回拼接后的时间字符串
  131. */
  132. export function formatAxis(param: Date): string {
  133. const hour: number = new Date(param).getHours()
  134. if (hour < 6) return '凌晨好'
  135. else if (hour < 9) return '早上好'
  136. else if (hour < 12) return '上午好'
  137. else if (hour < 14) return '中午好'
  138. else if (hour < 17) return '下午好'
  139. else if (hour < 19) return '傍晚好'
  140. else if (hour < 22) return '晚上好'
  141. else return '夜里好'
  142. }
  143. /**
  144. * 将毫秒,转换成时间字符串。例如说,xx 分钟
  145. *
  146. * @param ms 毫秒
  147. * @returns {string} 字符串
  148. */
  149. export function formatPast2(ms) {
  150. const day = Math.floor(ms / (24 * 60 * 60 * 1000))
  151. const hour = Math.floor(ms / (60 * 60 * 1000) - day * 24)
  152. const minute = Math.floor(ms / (60 * 1000) - day * 24 * 60 - hour * 60)
  153. const second = Math.floor(ms / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60)
  154. if (day > 0) {
  155. return day + '天' + hour + '小时' + minute + '分钟'
  156. }
  157. if (hour > 0) {
  158. return hour + '小时' + minute + '分钟'
  159. }
  160. if (minute > 0) {
  161. return minute + '分钟'
  162. }
  163. if (second > 0) {
  164. return second + '秒'
  165. } else {
  166. return 0 + '秒'
  167. }
  168. }
  169. /**
  170. * element plus 的时间 Formatter 实现,使用 YYYY-MM-DD HH:mm:ss 格式
  171. *
  172. * @param row 行数据
  173. * @param column 字段
  174. * @param cellValue 字段值
  175. */
  176. // @ts-ignore
  177. export const dateFormatter = (row, column, cellValue) => {
  178. if (!cellValue) {
  179. return
  180. }
  181. return dayjs(cellValue).format('YYYY-MM-DD HH:mm:ss')
  182. }