formatTime.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /**
  25. * 获取当前日期是第几周
  26. * @param dateTime 当前传入的日期值
  27. * @returns 返回第几周数字值
  28. */
  29. export function getWeek(dateTime: Date): number {
  30. const temptTime = new Date(dateTime.getTime())
  31. // 周几
  32. const weekday = temptTime.getDay() || 7
  33. // 周1+5天=周六
  34. temptTime.setDate(temptTime.getDate() - weekday + 1 + 5)
  35. let firstDay = new Date(temptTime.getFullYear(), 0, 1)
  36. const dayOfWeek = firstDay.getDay()
  37. let spendDay = 1
  38. if (dayOfWeek != 0) spendDay = 7 - dayOfWeek + 1
  39. firstDay = new Date(temptTime.getFullYear(), 0, 1 + spendDay)
  40. const d = Math.ceil((temptTime.valueOf() - firstDay.valueOf()) / 86400000)
  41. return Math.ceil(d / 7)
  42. }
  43. /**
  44. * 将时间转换为 `几秒前`、`几分钟前`、`几小时前`、`几天前`
  45. * @param param 当前时间,new Date() 格式或者字符串时间格式
  46. * @param format 需要转换的时间格式字符串
  47. * @description param 10秒: 10 * 1000
  48. * @description param 1分: 60 * 1000
  49. * @description param 1小时: 60 * 60 * 1000
  50. * @description param 24小时:60 * 60 * 24 * 1000
  51. * @description param 3天: 60 * 60* 24 * 1000 * 3
  52. * @returns 返回拼接后的时间字符串
  53. */
  54. export function formatPast(param: string | Date, format = 'YYYY-mm-dd HH:MM:SS'): string {
  55. // 传入格式处理、存储转换值
  56. let t: any, s: number
  57. // 获取js 时间戳
  58. let time: number = new Date().getTime()
  59. // 是否是对象
  60. typeof param === 'string' || 'object' ? (t = new Date(param).getTime()) : (t = param)
  61. // 当前时间戳 - 传入时间戳
  62. time = Number.parseInt(`${time - t}`)
  63. if (time < 10000) {
  64. // 10秒内
  65. return '刚刚'
  66. } else if (time < 60000 && time >= 10000) {
  67. // 超过10秒少于1分钟内
  68. s = Math.floor(time / 1000)
  69. return `${s}秒前`
  70. } else if (time < 3600000 && time >= 60000) {
  71. // 超过1分钟少于1小时
  72. s = Math.floor(time / 60000)
  73. return `${s}分钟前`
  74. } else if (time < 86400000 && time >= 3600000) {
  75. // 超过1小时少于24小时
  76. s = Math.floor(time / 3600000)
  77. return `${s}小时前`
  78. } else if (time < 259200000 && time >= 86400000) {
  79. // 超过1天少于3天内
  80. s = Math.floor(time / 86400000)
  81. return `${s}天前`
  82. } else {
  83. // 超过3天
  84. const date = typeof param === 'string' || 'object' ? new Date(param) : param
  85. return formatDate(date, format)
  86. }
  87. }
  88. /**
  89. * 时间问候语
  90. * @param param 当前时间,new Date() 格式
  91. * @description param 调用 `formatAxis(new Date())` 输出 `上午好`
  92. * @returns 返回拼接后的时间字符串
  93. */
  94. export function formatAxis(param: Date): string {
  95. const hour: number = new Date(param).getHours()
  96. if (hour < 6) return '凌晨好'
  97. else if (hour < 9) return '早上好'
  98. else if (hour < 12) return '上午好'
  99. else if (hour < 14) return '中午好'
  100. else if (hour < 17) return '下午好'
  101. else if (hour < 19) return '傍晚好'
  102. else if (hour < 22) return '晚上好'
  103. else return '夜里好'
  104. }
  105. /**
  106. * 将毫秒,转换成时间字符串。例如说,xx 分钟
  107. *
  108. * @param ms 毫秒
  109. * @returns {string} 字符串
  110. */
  111. export function formatPast2(ms) {
  112. const day = Math.floor(ms / (24 * 60 * 60 * 1000))
  113. const hour = Math.floor(ms / (60 * 60 * 1000) - day * 24)
  114. const minute = Math.floor(ms / (60 * 1000) - day * 24 * 60 - hour * 60)
  115. const second = Math.floor(ms / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60)
  116. if (day > 0) {
  117. return day + '天' + hour + '小时' + minute + '分钟'
  118. }
  119. if (hour > 0) {
  120. return hour + '小时' + minute + '分钟'
  121. }
  122. if (minute > 0) {
  123. return minute + '分钟'
  124. }
  125. if (second > 0) {
  126. return second + '秒'
  127. } else {
  128. return 0 + '秒'
  129. }
  130. }
  131. /**
  132. * element plus 的时间 Formatter 实现,使用 YYYY-MM-DD HH:mm:ss 格式
  133. *
  134. * @param row 行数据
  135. * @param column 字段
  136. * @param cellValue 字段值
  137. */
  138. // @ts-ignore
  139. export const dateFormatter = (row, column, cellValue) => {
  140. if (!cellValue) {
  141. return
  142. }
  143. return formatDate(cellValue)
  144. }
  145. /**
  146. * element plus 的时间 Formatter 实现,使用 YYYY-MM-DD 格式
  147. *
  148. * @param row 行数据
  149. * @param column 字段
  150. * @param cellValue 字段值
  151. */
  152. // @ts-ignore
  153. export const dateFormatter2 = (row, column, cellValue) => {
  154. if (!cellValue) {
  155. return
  156. }
  157. return formatDate(cellValue, 'YYYY-MM-DD')
  158. }
  159. /**
  160. * 设置起始日期,时间为00:00:00
  161. * @param param 传入日期
  162. * @returns 带时间00:00:00的日期
  163. */
  164. export function beginOfDay(param: Date) {
  165. return new Date(param.getFullYear(), param.getMonth(), param.getDate(), 0, 0, 0)
  166. }
  167. /**
  168. * 设置结束日期,时间为23:59:59
  169. * @param param 传入日期
  170. * @returns 带时间23:59:59的日期
  171. */
  172. export function endOfDay(param: Date) {
  173. return new Date(param.getFullYear(), param.getMonth(), param.getDate(), 23, 59, 59)
  174. }
  175. /**
  176. * 计算两个日期间隔天数
  177. * @param param1 日期1
  178. * @param param2 日期2
  179. */
  180. export function betweenDay(param1: Date, param2: Date) {
  181. param1 = convertDate(param1)
  182. param2 = convertDate(param2)
  183. // 计算差值
  184. return Math.floor((param2.getTime() - param1.getTime()) / (24 * 3600 * 1000))
  185. }
  186. /**
  187. * 日期计算
  188. * @param param1 日期
  189. * @param param2 添加的时间
  190. */
  191. export function addTime(param1: Date, param2: number) {
  192. param1 = convertDate(param1)
  193. return new Date(param1.getTime() + param2)
  194. }
  195. /**
  196. * 日期转换
  197. * @param param 日期
  198. */
  199. export function convertDate(param: Date | string) {
  200. if (typeof param === 'string') {
  201. return new Date(param)
  202. }
  203. return param
  204. }