utils.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import type { Dayjs } from 'dayjs'
  2. import type { Day } from './types'
  3. const monthMaps: Record<string, Day[]> = {}
  4. export const cloneTime = (targetDate: Dayjs, sourceDate: Dayjs) => {
  5. return targetDate.clone()
  6. .set('hour', sourceDate.hour())
  7. .set('minute', sourceDate.minute())
  8. }
  9. export const getDaysInMonth = (currentDate: Dayjs) => {
  10. const key = currentDate.format('YYYY-MM')
  11. // return the cached days
  12. if (monthMaps[key])
  13. return monthMaps[key]
  14. const daysInCurrentMonth = currentDate.daysInMonth()
  15. const firstDay = currentDate.startOf('month').day()
  16. const lastDay = currentDate.endOf('month').day()
  17. const lastDayInLastMonth = currentDate.clone().subtract(1, 'month').endOf('month')
  18. const firstDayInNextMonth = currentDate.clone().add(1, 'month').startOf('month')
  19. const days: Day[] = []
  20. const daysInOneWeek = 7
  21. const totalLines = 6
  22. // Add cells for days before the first day of the month
  23. for (let i = firstDay - 1; i >= 0; i--) {
  24. const date = cloneTime(lastDayInLastMonth.subtract(i, 'day'), currentDate)
  25. days.push({
  26. date,
  27. isCurrentMonth: false,
  28. })
  29. }
  30. // Add days of the month
  31. for (let i = 1; i <= daysInCurrentMonth; i++) {
  32. const date = cloneTime(currentDate.startOf('month').add(i - 1, 'day'), currentDate)
  33. days.push({
  34. date,
  35. isCurrentMonth: true,
  36. })
  37. }
  38. // Add cells for days after the last day of the month
  39. const totalLinesOfCurrentMonth = Math.ceil((daysInCurrentMonth - ((daysInOneWeek - firstDay) + lastDay + 1)) / 7) + 2
  40. const needAdditionalLine = totalLinesOfCurrentMonth < totalLines
  41. for (let i = 0; lastDay + i < (needAdditionalLine ? 2 * daysInOneWeek - 1 : daysInOneWeek - 1); i++) {
  42. const date = cloneTime(firstDayInNextMonth.add(i, 'day'), currentDate)
  43. days.push({
  44. date,
  45. isCurrentMonth: false,
  46. })
  47. }
  48. // cache the days
  49. monthMaps[key] = days
  50. return days
  51. }
  52. export const getHourIn12Hour = (date: Dayjs) => {
  53. const hour = date.hour()
  54. return hour === 0 ? 12 : hour >= 12 ? hour - 12 : hour
  55. }