formatTime.ts 6.0 KB

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