dayjs.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. dayjs.extend(utc)
  6. dayjs.extend(timezone)
  7. export default dayjs
  8. const monthMaps: Record<string, Day[]> = {}
  9. export const cloneTime = (targetDate: Dayjs, sourceDate: Dayjs) => {
  10. return targetDate.clone()
  11. .set('hour', sourceDate.hour())
  12. .set('minute', sourceDate.minute())
  13. }
  14. export const getDaysInMonth = (currentDate: Dayjs) => {
  15. const key = currentDate.format('YYYY-MM')
  16. // return the cached days
  17. if (monthMaps[key])
  18. return monthMaps[key]
  19. const daysInCurrentMonth = currentDate.daysInMonth()
  20. const firstDay = currentDate.startOf('month').day()
  21. const lastDay = currentDate.endOf('month').day()
  22. const lastDayInLastMonth = currentDate.clone().subtract(1, 'month').endOf('month')
  23. const firstDayInNextMonth = currentDate.clone().add(1, 'month').startOf('month')
  24. const days: Day[] = []
  25. const daysInOneWeek = 7
  26. const totalLines = 6
  27. // Add cells for days before the first day of the month
  28. for (let i = firstDay - 1; i >= 0; i--) {
  29. const date = cloneTime(lastDayInLastMonth.subtract(i, 'day'), currentDate)
  30. days.push({
  31. date,
  32. isCurrentMonth: false,
  33. })
  34. }
  35. // Add days of the month
  36. for (let i = 1; i <= daysInCurrentMonth; i++) {
  37. const date = cloneTime(currentDate.startOf('month').add(i - 1, 'day'), currentDate)
  38. days.push({
  39. date,
  40. isCurrentMonth: true,
  41. })
  42. }
  43. // Add cells for days after the last day of the month
  44. const totalLinesOfCurrentMonth = Math.ceil((daysInCurrentMonth - ((daysInOneWeek - firstDay) + lastDay + 1)) / 7) + 2
  45. const needAdditionalLine = totalLinesOfCurrentMonth < totalLines
  46. for (let i = 0; lastDay + i < (needAdditionalLine ? 2 * daysInOneWeek - 1 : daysInOneWeek - 1); i++) {
  47. const date = cloneTime(firstDayInNextMonth.add(i, 'day'), currentDate)
  48. days.push({
  49. date,
  50. isCurrentMonth: false,
  51. })
  52. }
  53. // cache the days
  54. monthMaps[key] = days
  55. return days
  56. }
  57. export const clearMonthMapCache = () => {
  58. for (const key in monthMaps)
  59. delete monthMaps[key]
  60. }
  61. export const getHourIn12Hour = (date: Dayjs) => {
  62. const hour = date.hour()
  63. return hour === 0 ? 12 : hour >= 12 ? hour - 12 : hour
  64. }
  65. export const getDateWithTimezone = (props: { date?: Dayjs, timezone?: string }) => {
  66. return props.date ? dayjs.tz(props.date, props.timezone) : dayjs().tz(props.timezone)
  67. }