formatTime.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. import dayjs from 'dayjs'
  2. import type { TableColumnCtx } from 'element-plus'
  3. /**
  4. * 日期快捷选项适用于 el-date-picker
  5. */
  6. export const rangeShortcuts = [
  7. {
  8. text: '今天',
  9. value: () => {
  10. const today = dayjs()
  11. return [today.startOf('day').toDate(), today.endOf('day').toDate()]
  12. }
  13. },
  14. {
  15. text: '昨天',
  16. value: () => {
  17. const yesterday = dayjs().subtract(1, 'day')
  18. return [yesterday.startOf('day').toDate(), yesterday.endOf('day').toDate()]
  19. }
  20. },
  21. {
  22. text: '本周',
  23. value: () => {
  24. return [dayjs().startOf('week').toDate(), dayjs().endOf('week').toDate()]
  25. }
  26. },
  27. {
  28. text: '上周',
  29. value: () => {
  30. const lastWeek = dayjs().subtract(1, 'week')
  31. return [lastWeek.startOf('week').toDate(), lastWeek.endOf('week').toDate()]
  32. }
  33. },
  34. {
  35. text: '本月',
  36. value: () => {
  37. return [dayjs().startOf('month').toDate(), dayjs().endOf('month').toDate()]
  38. }
  39. },
  40. {
  41. text: '上月',
  42. value: () => {
  43. const lastMonth = dayjs().subtract(1, 'month')
  44. return [lastMonth.startOf('month').toDate(), lastMonth.endOf('month').toDate()]
  45. }
  46. },
  47. {
  48. text: '本季度',
  49. value: () => {
  50. return [dayjs().startOf('quarter').toDate(), dayjs().endOf('quarter').toDate()]
  51. }
  52. },
  53. {
  54. text: '上季度',
  55. value: () => {
  56. const lastQuarter = dayjs().subtract(1, 'quarter')
  57. return [lastQuarter.startOf('quarter').toDate(), lastQuarter.endOf('quarter').toDate()]
  58. }
  59. },
  60. {
  61. text: '今年',
  62. value: () => {
  63. return [dayjs().startOf('year').toDate(), dayjs().endOf('year').toDate()]
  64. }
  65. },
  66. {
  67. text: '去年',
  68. value: () => {
  69. const lastYear = dayjs().subtract(1, 'year')
  70. return [lastYear.startOf('year').toDate(), lastYear.endOf('year').toDate()]
  71. }
  72. },
  73. {
  74. text: '最近7天',
  75. value: () => {
  76. return [dayjs().subtract(6, 'day').toDate(), dayjs().toDate()]
  77. }
  78. },
  79. {
  80. text: '最近30天',
  81. value: () => {
  82. return [dayjs().subtract(29, 'day').toDate(), dayjs().toDate()]
  83. }
  84. },
  85. {
  86. text: '最近90天',
  87. value: () => {
  88. return [dayjs().subtract(89, 'day').toDate(), dayjs().toDate()]
  89. }
  90. },
  91. {
  92. text: '最近一年',
  93. value: () => {
  94. return [dayjs().subtract(1, 'year').toDate(), dayjs().toDate()]
  95. }
  96. }
  97. ]
  98. export const defaultShortcuts = [
  99. {
  100. text: '今天',
  101. value: () => {
  102. return new Date()
  103. }
  104. },
  105. {
  106. text: '昨天',
  107. value: () => {
  108. const date = new Date()
  109. date.setTime(date.getTime() - 3600 * 1000 * 24)
  110. return [date, date]
  111. }
  112. },
  113. {
  114. text: '最近七天',
  115. value: () => {
  116. const date = new Date()
  117. date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
  118. return [date, new Date()]
  119. }
  120. },
  121. {
  122. text: '最近 30 天',
  123. value: () => {
  124. const date = new Date()
  125. date.setTime(date.getTime() - 3600 * 1000 * 24 * 30)
  126. return [date, new Date()]
  127. }
  128. },
  129. {
  130. text: '本月',
  131. value: () => {
  132. const date = new Date()
  133. date.setDate(1) // 设置为当前月的第一天
  134. return [date, new Date()]
  135. }
  136. },
  137. {
  138. text: '今年',
  139. value: () => {
  140. const date = new Date()
  141. return [new Date(`${date.getFullYear()}-01-01`), date]
  142. }
  143. }
  144. ]
  145. /**
  146. * 时间日期转换
  147. * @param date 当前时间,new Date() 格式
  148. * @param format 需要转换的时间格式字符串
  149. * @description format 字符串随意,如 `YYYY-MM、YYYY-MM-DD`
  150. * @description format 季度:"YYYY-MM-DD HH:mm:ss QQQQ"
  151. * @description format 星期:"YYYY-MM-DD HH:mm:ss WWW"
  152. * @description format 几周:"YYYY-MM-DD HH:mm:ss ZZZ"
  153. * @description format 季度 + 星期 + 几周:"YYYY-MM-DD HH:mm:ss WWW QQQQ ZZZ"
  154. * @returns 返回拼接后的时间字符串
  155. */
  156. export function formatDate(date: Date, format?: string): string {
  157. // 日期不存在,则返回空
  158. if (!date) {
  159. return ''
  160. }
  161. // 日期存在,则进行格式化
  162. return date ? dayjs(date).format(format ?? 'YYYY-MM-DD HH:mm:ss') : ''
  163. }
  164. /**
  165. * 获取当前的日期+时间
  166. */
  167. export function getNowDateTime() {
  168. return dayjs()
  169. }
  170. /**
  171. * 获取当前日期是第几周
  172. * @param dateTime 当前传入的日期值
  173. * @returns 返回第几周数字值
  174. */
  175. export function getWeek(dateTime: Date): number {
  176. const temptTime = new Date(dateTime.getTime())
  177. // 周几
  178. const weekday = temptTime.getDay() || 7
  179. // 周1+5天=周六
  180. temptTime.setDate(temptTime.getDate() - weekday + 1 + 5)
  181. let firstDay = new Date(temptTime.getFullYear(), 0, 1)
  182. const dayOfWeek = firstDay.getDay()
  183. let spendDay = 1
  184. if (dayOfWeek != 0) spendDay = 7 - dayOfWeek + 1
  185. firstDay = new Date(temptTime.getFullYear(), 0, 1 + spendDay)
  186. const d = Math.ceil((temptTime.valueOf() - firstDay.valueOf()) / 86400000)
  187. return Math.ceil(d / 7)
  188. }
  189. /**
  190. * 将时间转换为 `几秒前`、`几分钟前`、`几小时前`、`几天前`
  191. * @param param 当前时间,new Date() 格式或者字符串时间格式
  192. * @param format 需要转换的时间格式字符串
  193. * @description param 10秒: 10 * 1000
  194. * @description param 1分: 60 * 1000
  195. * @description param 1小时: 60 * 60 * 1000
  196. * @description param 24小时:60 * 60 * 24 * 1000
  197. * @description param 3天: 60 * 60* 24 * 1000 * 3
  198. * @returns 返回拼接后的时间字符串
  199. */
  200. export function formatPast(param: string | Date, format = 'YYYY-MM-DD HH:mm:ss'): string {
  201. // 传入格式处理、存储转换值
  202. let t: any, s: number
  203. // 获取js 时间戳
  204. let time: number = new Date().getTime()
  205. // 是否是对象
  206. typeof param === 'string' || 'object' ? (t = new Date(param).getTime()) : (t = param)
  207. // 当前时间戳 - 传入时间戳
  208. time = Number.parseInt(`${time - t}`)
  209. if (time < 10000) {
  210. // 10秒内
  211. return '刚刚'
  212. } else if (time < 60000 && time >= 10000) {
  213. // 超过10秒少于1分钟内
  214. s = Math.floor(time / 1000)
  215. return `${s}秒前`
  216. } else if (time < 3600000 && time >= 60000) {
  217. // 超过1分钟少于1小时
  218. s = Math.floor(time / 60000)
  219. return `${s}分钟前`
  220. } else if (time < 86400000 && time >= 3600000) {
  221. // 超过1小时少于24小时
  222. s = Math.floor(time / 3600000)
  223. return `${s}小时前`
  224. } else if (time < 259200000 && time >= 86400000) {
  225. // 超过1天少于3天内
  226. s = Math.floor(time / 86400000)
  227. return `${s}天前`
  228. } else {
  229. // 超过3天
  230. const date = typeof param === 'string' || 'object' ? new Date(param) : param
  231. return formatDate(date, format)
  232. }
  233. }
  234. /**
  235. * 时间问候语
  236. * @param param 当前时间,new Date() 格式
  237. * @description param 调用 `formatAxis(new Date())` 输出 `上午好`
  238. * @returns 返回拼接后的时间字符串
  239. */
  240. export function formatAxis(param: Date): string {
  241. const hour: number = new Date(param).getHours()
  242. if (hour < 6) return '凌晨好'
  243. else if (hour < 9) return '早上好'
  244. else if (hour < 12) return '上午好'
  245. else if (hour < 14) return '中午好'
  246. else if (hour < 17) return '下午好'
  247. else if (hour < 19) return '傍晚好'
  248. else if (hour < 22) return '晚上好'
  249. else return '夜里好'
  250. }
  251. /**
  252. * 将毫秒,转换成时间字符串。例如说,xx 分钟
  253. *
  254. * @param ms 毫秒
  255. * @returns {string} 字符串
  256. */
  257. export function formatPast2(ms: number): string {
  258. const day = Math.floor(ms / (24 * 60 * 60 * 1000))
  259. const hour = Math.floor(ms / (60 * 60 * 1000) - day * 24)
  260. const minute = Math.floor(ms / (60 * 1000) - day * 24 * 60 - hour * 60)
  261. const second = Math.floor(ms / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60)
  262. if (day > 0) {
  263. return day + ' 天' + hour + ' 小时 ' + minute + ' 分钟'
  264. }
  265. if (hour > 0) {
  266. return hour + ' 小时 ' + minute + ' 分钟'
  267. }
  268. if (minute > 0) {
  269. return minute + ' 分钟'
  270. }
  271. if (second > 0) {
  272. return second + ' 秒'
  273. } else {
  274. return 0 + ' 秒'
  275. }
  276. }
  277. /**
  278. * element plus 的时间 Formatter 实现,使用 YYYY-MM-DD HH:mm:ss 格式
  279. *
  280. * @param row 行数据
  281. * @param column 字段
  282. * @param cellValue 字段值
  283. */
  284. export function dateFormatter(_row: any, _column: TableColumnCtx<any> | null, cellValue: any): string {
  285. return cellValue ? formatDate(cellValue) : ''
  286. }
  287. /**
  288. * element plus 的时间 Formatter 实现,使用 YYYY-MM-DD 格式
  289. *
  290. * @param row 行数据
  291. * @param column 字段
  292. * @param cellValue 字段值
  293. */
  294. export function dateFormatter2(_row: any, _column: TableColumnCtx<any> |null, cellValue: any): string {
  295. return cellValue ? formatDate(cellValue, 'YYYY-MM-DD') : ''
  296. }
  297. /**
  298. * 设置起始日期,时间为00:00:00
  299. * @param param 传入日期
  300. * @returns 带时间00:00:00的日期
  301. */
  302. export function beginOfDay(param: Date): Date {
  303. return new Date(param.getFullYear(), param.getMonth(), param.getDate(), 0, 0, 0)
  304. }
  305. /**
  306. * 设置结束日期,时间为23:59:59
  307. * @param param 传入日期
  308. * @returns 带时间23:59:59的日期
  309. */
  310. export function endOfDay(param: Date): Date {
  311. return new Date(param.getFullYear(), param.getMonth(), param.getDate(), 23, 59, 59)
  312. }
  313. /**
  314. * 计算两个日期间隔天数
  315. * @param param1 日期1
  316. * @param param2 日期2
  317. */
  318. export function betweenDay(param1: Date, param2: Date): number {
  319. param1 = convertDate(param1)
  320. param2 = convertDate(param2)
  321. // 计算差值
  322. return Math.floor((param2.getTime() - param1.getTime()) / (24 * 3600 * 1000))
  323. }
  324. /**
  325. * 日期计算
  326. * @param param1 日期
  327. * @param param2 添加的时间
  328. */
  329. export function addTime(param1: Date, param2: number): Date {
  330. param1 = convertDate(param1)
  331. return new Date(param1.getTime() + param2)
  332. }
  333. /**
  334. * 日期转换
  335. * @param param 日期
  336. */
  337. export function convertDate(param: Date | string): Date {
  338. if (typeof param === 'string') {
  339. return new Date(param)
  340. }
  341. return param
  342. }
  343. /**
  344. * 指定的两个日期, 是否为同一天
  345. * @param a 日期 A
  346. * @param b 日期 B
  347. */
  348. export function isSameDay(a: dayjs.ConfigType, b: dayjs.ConfigType): boolean {
  349. if (!a || !b) return false
  350. const aa = dayjs(a)
  351. const bb = dayjs(b)
  352. return aa.year() == bb.year() && aa.month() == bb.month() && aa.day() == bb.day()
  353. }
  354. /**
  355. * 获取一天的开始时间、截止时间
  356. * @param date 日期
  357. * @param days 天数
  358. */
  359. export function getDayRange(
  360. date: dayjs.ConfigType,
  361. days: number
  362. ): [dayjs.ConfigType, dayjs.ConfigType] {
  363. const day = dayjs(date).add(days, 'd')
  364. return getDateRange(day, day)
  365. }
  366. /**
  367. * 获取最近7天的开始时间、截止时间
  368. */
  369. export function getLast7Days(): [dayjs.ConfigType, dayjs.ConfigType] {
  370. const lastWeekDay = dayjs().subtract(7, 'd')
  371. const yesterday = dayjs().subtract(1, 'd')
  372. return getDateRange(lastWeekDay, yesterday)
  373. }
  374. /**
  375. * 获取最近30天的开始时间、截止时间
  376. */
  377. export function getLast30Days(): [dayjs.ConfigType, dayjs.ConfigType] {
  378. const lastMonthDay = dayjs().subtract(30, 'd')
  379. const yesterday = dayjs().subtract(1, 'd')
  380. return getDateRange(lastMonthDay, yesterday)
  381. }
  382. /**
  383. * 获取最近1年的开始时间、截止时间
  384. */
  385. export function getLast1Year(): [dayjs.ConfigType, dayjs.ConfigType] {
  386. const lastYearDay = dayjs().subtract(1, 'y')
  387. const yesterday = dayjs().subtract(1, 'd')
  388. return getDateRange(lastYearDay, yesterday)
  389. }
  390. /**
  391. * 获取指定日期的开始时间、截止时间
  392. * @param beginDate 开始日期
  393. * @param endDate 截止日期
  394. */
  395. export function getDateRange(
  396. beginDate: dayjs.ConfigType,
  397. endDate: dayjs.ConfigType
  398. ): [string, string] {
  399. return [
  400. dayjs(beginDate).startOf('d').format('YYYY-MM-DD HH:mm:ss'),
  401. dayjs(endDate).endOf('d').format('YYYY-MM-DD HH:mm:ss')
  402. ]
  403. }