react-day-picker
Version:
Customizable Date Picker for React
56 lines (50 loc) • 1.58 kB
text/typescript
import type { Locale } from 'date-fns';
import addWeeks from 'date-fns/addWeeks';
import endOfMonth from 'date-fns/endOfMonth';
import getWeeksInMonth from 'date-fns/getWeeksInMonth';
import startOfMonth from 'date-fns/startOfMonth';
import { daysToMonthWeeks } from './daysToMonthWeeks';
/** Represents a week in the month.*/
export type MonthWeek = {
/** The week number from the start of the year. */
weekNumber: number;
/** The dates in the week. */
dates: Date[];
};
/**
* Return the weeks belonging to the given month, adding the "outside days" to
* the first and last week.
*/
export function getMonthWeeks(
/** The month to get the weeks from */
month: Date,
options: {
locale: Locale;
/** Add extra weeks up to 6 weeks */
useFixedWeeks?: boolean;
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7;
}
): MonthWeek[] {
const weeksInMonth: MonthWeek[] = daysToMonthWeeks(
startOfMonth(month),
endOfMonth(month),
options
);
// Add extra weeks to the month, up to 6 weeks
if (options?.useFixedWeeks) {
const nrOfMonthWeeks = getWeeksInMonth(month, options);
if (nrOfMonthWeeks < 6) {
const lastWeek = weeksInMonth[weeksInMonth.length - 1];
const lastDate = lastWeek.dates[lastWeek.dates.length - 1];
const toDate = addWeeks(lastDate, 6 - nrOfMonthWeeks);
const extraWeeks = daysToMonthWeeks(
addWeeks(lastDate, 1),
toDate,
options
);
weeksInMonth.push(...extraWeeks);
}
}
return weeksInMonth;
}