Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use dateLib for getting days/months/years from a Date #2643

Merged
merged 1 commit into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/DayPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ export function DayPicker(props: DayPickerProps) {
onChange={handleMonthChange(calendarMonth.date)}
options={dropdownMonths}
style={styles?.[UI.Dropdown]}
value={calendarMonth.date.getMonth()}
value={dateLib.getMonth(calendarMonth.date)}
/>
) : (
<span role="status" aria-live="polite">
Expand All @@ -367,7 +367,7 @@ export function DayPicker(props: DayPickerProps) {
onChange={handleYearChange(calendarMonth.date)}
options={dropdownYears}
style={styles?.[UI.Dropdown]}
value={calendarMonth.date.getFullYear()}
value={dateLib.getYear(calendarMonth.date)}
/>
) : (
<span role="status" aria-live="polite">
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/getBroadcastWeeksInMonth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function getBroadcastWeeksInMonth(month: Date, dateLib: DateLib): 4 | 5 {
FIVE_WEEKS * 7 - 1
);
const numberOfWeeks =
month.getMonth() === lastDateOfLastWeek.getMonth()
dateLib.getMonth(month) === dateLib.getMonth(lastDateOfLastWeek)
? FIVE_WEEKS
: FOUR_WEEKS;

Expand Down
24 changes: 9 additions & 15 deletions src/helpers/getNavMonth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { TZDate } from "@date-fns/tz";

import type { DateLib } from "../classes/DateLib.js";
import type { DayPickerProps } from "../types/index.js";

Expand Down Expand Up @@ -28,7 +26,9 @@ export function getNavMonths(
startOfMonth,
endOfMonth,
addYears,
endOfYear
endOfYear,
newDate,
today
} = dateLib;

// Handle deprecated code
Expand All @@ -37,35 +37,29 @@ export function getNavMonths(
startMonth = fromMonth;
}
if (!startMonth && fromYear) {
startMonth = new dateLib.Date(fromYear, 0, 1);
startMonth = dateLib.newDate(fromYear, 0, 1);
}
if (!endMonth && toMonth) {
endMonth = toMonth;
}
if (!endMonth && toYear) {
endMonth = new dateLib.Date(toYear, 11, 31);
endMonth = newDate(toYear, 11, 31);
}

const hasDropdowns = props.captionLayout?.startsWith("dropdown");
if (startMonth) {
startMonth = startOfMonth(startMonth);
} else if (fromYear) {
startMonth = new dateLib.Date(fromYear, 0, 1);
startMonth = newDate(fromYear, 0, 1);
} else if (!startMonth && hasDropdowns) {
const today =
props.today ??
(props.timeZone ? TZDate.tz(props.timeZone) : new dateLib.Date());
startMonth = startOfYear(addYears(today, -100));
startMonth = startOfYear(addYears(props.today ?? today(), -100));
}
if (endMonth) {
endMonth = endOfMonth(endMonth);
} else if (toYear) {
endMonth = new dateLib.Date(toYear, 11, 31);
endMonth = newDate(toYear, 11, 31);
} else if (!endMonth && hasDropdowns) {
const today =
props.today ??
(props.timeZone ? TZDate.tz(props.timeZone) : new dateLib.Date());
endMonth = endOfYear(today);
endMonth = endOfYear(props.today ?? today());
}
return [
startMonth ? startOfDay(startMonth) : startMonth,
Expand Down
11 changes: 4 additions & 7 deletions src/helpers/getWeekdays.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { TZDate } from "@date-fns/tz";

import { DateLib } from "../classes/DateLib.js";

/**
Expand All @@ -11,17 +9,16 @@ export function getWeekdays(
dateLib: DateLib,
/** Use ISOWeek instead of locale/ */
ISOWeek?: boolean | undefined,
timeZone?: string | undefined,
/** @since 9.4.0 */
broadcastCalendar?: boolean | undefined
): Date[] {
const date = timeZone ? TZDate.tz(timeZone) : new dateLib.Date();
const today = dateLib.today();

const start = broadcastCalendar
? dateLib.startOfBroadcastWeek(date, dateLib)
? dateLib.startOfBroadcastWeek(today, dateLib)
: ISOWeek
? dateLib.startOfISOWeek(date)
: dateLib.startOfWeek(date);
? dateLib.startOfISOWeek(today)
: dateLib.startOfWeek(today);

const days: Date[] = [];
for (let i = 0; i < 7; i++) {
Expand Down
Loading