Skip to content

Commit 7790e13

Browse files
Merge pull request #6106 from Hacker0x01/fix/issue-6105-timezone-holiday-parsing
fix: parse holiday date strings as local time to prevent timezone shift
2 parents 69da2fe + f7c928e commit 7790e13

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,12 @@ export class DatePicker extends Component<DatePickerProps, DatePickerState> {
419419
: newDate();
420420

421421
// Convert the date from string format to standard Date format
422+
// Uses parseDate with ISO format to parse as local time, preventing
423+
// dates from shifting in timezones west of UTC. See issue #6105.
422424
modifyHolidays = () =>
423425
this.props.holidays?.reduce<HolidayItem[]>((accumulator, holiday) => {
424-
const date = new Date(holiday.date);
425-
if (!isValid(date)) {
426+
const date = parseDate(holiday.date, "yyyy-MM-dd", undefined, false);
427+
if (!date) {
426428
return accumulator;
427429
}
428430

src/test/datepicker_test.test.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5541,6 +5541,40 @@ describe("DatePicker", () => {
55415541
expect(container.querySelector(".react-datepicker")).not.toBeNull();
55425542
});
55435543

5544+
it("should apply holiday class to correct date regardless of timezone (issue #6105)", () => {
5545+
// This test verifies that holidays specified as ISO date strings (YYYY-MM-DD)
5546+
// are displayed on the correct date. The bug was that new Date("YYYY-MM-DD")
5547+
// parses as UTC midnight, causing dates to shift in western timezones.
5548+
const holidays = [{ date: "2024-01-15", holidayName: "Test Holiday" }];
5549+
5550+
const { container } = render(
5551+
<DatePicker
5552+
selected={new Date(2024, 0, 15)} // January 15, 2024 in local time
5553+
onChange={() => {}}
5554+
holidays={holidays}
5555+
inline
5556+
/>,
5557+
);
5558+
5559+
// Find the day element for January 15th and verify it has the holiday class
5560+
const jan15 = container.querySelector(
5561+
".react-datepicker__day--015:not(.react-datepicker__day--outside-month)",
5562+
);
5563+
expect(jan15).not.toBeNull();
5564+
expect(jan15?.classList.contains("react-datepicker__day--holidays")).toBe(
5565+
true,
5566+
);
5567+
5568+
// Verify January 14th does NOT have the holiday class (the bug would show it here)
5569+
const jan14 = container.querySelector(
5570+
".react-datepicker__day--014:not(.react-datepicker__day--outside-month)",
5571+
);
5572+
expect(jan14).not.toBeNull();
5573+
expect(jan14?.classList.contains("react-datepicker__day--holidays")).toBe(
5574+
false,
5575+
);
5576+
});
5577+
55445578
it("should handle deferFocusInput and cancelFocusInput", () => {
55455579
jest.useFakeTimers();
55465580

0 commit comments

Comments
 (0)