formatTime.ts 9.1 KB

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