* Calculate the week index (0-based) and day index (0-based) for a given date within a month in a * calendar. * * @param date - The date to calculate indices for. * @param locale - The locale string (e.g., 'en-US', 'fr-FR', 'hi-IN-u-ca-indian'). * @param firstDayOfWeek - Optional override for t
(date: CalendarDate, locale: string, firstDayOfWeek?: DayOfWeek)
| 764 | * @returns Object with weekIndex and dayIndex. |
| 765 | */ |
| 766 | function useWeekAndDayIndices(date: CalendarDate, locale: string, firstDayOfWeek?: DayOfWeek) { |
| 767 | let result = useMemo(() => { |
| 768 | // Get the day index within the week (0-6) |
| 769 | const dayIndex = getDayOfWeek(date, locale, firstDayOfWeek); |
| 770 | |
| 771 | const monthStart = startOfMonth(date); |
| 772 | |
| 773 | // Calculate the week index by finding which week this date falls into |
| 774 | // within the month's calendar grid |
| 775 | const monthStartDayOfWeek = getDayOfWeek(monthStart, locale, firstDayOfWeek); |
| 776 | const dayOfMonth = date.day; |
| 777 | |
| 778 | const weekIndex = Math.floor((dayOfMonth + monthStartDayOfWeek - 1) / 7); |
| 779 | const lastDayOfMonth = startOfMonth(date).add({months: 1}).subtract({days: 1}); |
| 780 | const lastWeekIndex = Math.floor((lastDayOfMonth.day + monthStartDayOfWeek - 1) / 7); |
| 781 | |
| 782 | return { |
| 783 | weekIndex, |
| 784 | lastWeekIndex, |
| 785 | dayIndex |
| 786 | }; |
| 787 | }, [date, locale, firstDayOfWeek]); |
| 788 | |
| 789 | return result; |
| 790 | } |
no test coverage detected