formatTime.ts 6.3 KB

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