You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dayjs.ts 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import dayjs, { type Dayjs } from 'dayjs'
  2. import type { Day } from '../types'
  3. import utc from 'dayjs/plugin/utc'
  4. import timezone from 'dayjs/plugin/timezone'
  5. import tz from '@/utils/timezone.json'
  6. dayjs.extend(utc)
  7. dayjs.extend(timezone)
  8. export default dayjs
  9. const monthMaps: Record<string, Day[]> = {}
  10. export const cloneTime = (targetDate: Dayjs, sourceDate: Dayjs) => {
  11. return targetDate.clone()
  12. .set('hour', sourceDate.hour())
  13. .set('minute', sourceDate.minute())
  14. }
  15. export const getDaysInMonth = (currentDate: Dayjs) => {
  16. const key = currentDate.format('YYYY-MM')
  17. // return the cached days
  18. if (monthMaps[key])
  19. return monthMaps[key]
  20. const daysInCurrentMonth = currentDate.daysInMonth()
  21. const firstDay = currentDate.startOf('month').day()
  22. const lastDay = currentDate.endOf('month').day()
  23. const lastDayInLastMonth = currentDate.clone().subtract(1, 'month').endOf('month')
  24. const firstDayInNextMonth = currentDate.clone().add(1, 'month').startOf('month')
  25. const days: Day[] = []
  26. const daysInOneWeek = 7
  27. const totalLines = 6
  28. // Add cells for days before the first day of the month
  29. for (let i = firstDay - 1; i >= 0; i--) {
  30. const date = cloneTime(lastDayInLastMonth.subtract(i, 'day'), currentDate)
  31. days.push({
  32. date,
  33. isCurrentMonth: false,
  34. })
  35. }
  36. // Add days of the month
  37. for (let i = 1; i <= daysInCurrentMonth; i++) {
  38. const date = cloneTime(currentDate.startOf('month').add(i - 1, 'day'), currentDate)
  39. days.push({
  40. date,
  41. isCurrentMonth: true,
  42. })
  43. }
  44. // Add cells for days after the last day of the month
  45. const totalLinesOfCurrentMonth = Math.ceil((daysInCurrentMonth - ((daysInOneWeek - firstDay) + lastDay + 1)) / 7) + 2
  46. const needAdditionalLine = totalLinesOfCurrentMonth < totalLines
  47. for (let i = 0; lastDay + i < (needAdditionalLine ? 2 * daysInOneWeek - 1 : daysInOneWeek - 1); i++) {
  48. const date = cloneTime(firstDayInNextMonth.add(i, 'day'), currentDate)
  49. days.push({
  50. date,
  51. isCurrentMonth: false,
  52. })
  53. }
  54. // cache the days
  55. monthMaps[key] = days
  56. return days
  57. }
  58. export const clearMonthMapCache = () => {
  59. for (const key in monthMaps)
  60. delete monthMaps[key]
  61. }
  62. export const getHourIn12Hour = (date: Dayjs) => {
  63. const hour = date.hour()
  64. return hour === 0 ? 12 : hour >= 12 ? hour - 12 : hour
  65. }
  66. export const getDateWithTimezone = (props: { date?: Dayjs, timezone?: string }) => {
  67. return props.date ? dayjs.tz(props.date, props.timezone) : dayjs().tz(props.timezone)
  68. }
  69. // Asia/Shanghai -> UTC+8
  70. const DEFAULT_OFFSET_STR = 'UTC+0'
  71. export const convertTimezoneToOffsetStr = (timezone?: string) => {
  72. if (!timezone)
  73. return DEFAULT_OFFSET_STR
  74. const tzItem = tz.find(item => item.value === timezone)
  75. if(!tzItem)
  76. return DEFAULT_OFFSET_STR
  77. return `UTC${tzItem.name.charAt(0)}${tzItem.name.charAt(2)}`
  78. }