MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useWeekAndDayIndices

Function useWeekAndDayIndices

packages/@react-spectrum/s2/src/Calendar.tsx:766–790  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

764 * @returns Object with weekIndex and dayIndex.
765 */
766function 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}

Callers 1

CalendarCellFunction · 0.85

Calls 4

getDayOfWeekFunction · 0.90
startOfMonthFunction · 0.90
addMethod · 0.65
subtractMethod · 0.45

Tested by

no test coverage detected