-
-
Notifications
You must be signed in to change notification settings - Fork 754
fix: prevent timezone override when initialMonth is Date type #2737
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
Conversation
When initialMonth is passed as Date type, it would override the provided timezone settings. This fix ensures proper timezone handling.
Thanks @lovebuizel for your contribution! I believe the behavior of DayPicker is correct here. To pass a time-zoned date as the initial month, you should initialize the date with import { DayPicker, TZDate } from "react-day-picker";
export function TimeZone() {
const timeZone = "Pacific/Kiritimati";
return (
<DayPicker
// make sure you use `TZDate` here
month={new TZDate(2024, 1, 0, timeZone)}
/>
);
} See: https://daypicker.dev/docs/time-zone#working-with-time-zoned-dates We might want to further clarify this in the documentation. |
Thank you @gpbl for your response! I have a concern regarding the timezone configuration. I believe the timezone should be specified in only one place to avoid potential conflicts. For example, if I pass Interestingly, I've noticed that when I don't explicitly pass the timeZone parameter but provide a month as a TZDate object with its own timezone, the timezone from the TZDate object is correctly applied. This behavior essentially renders the separate timeZone parameter redundant and potentially misleading. This situation not only makes debugging challenging but also presents a potential issue where type checking doesn't catch the inconsistency when passing both a timezone parameter and a month parameter that isn't a TZDate type. |
I see your point @lovebuizel and I agree with your feedback here. I wonder what happens with the other props, such as |
Thanks @gpbl for your response! The
Within the
Regardless of which parameter is used, they all ultimately contribute to determining the However, there's an interesting edge case to consider: when no explicit This approach would provide a more intuitive and predictable behavior while maintaining timezone awareness throughout the component. What's your perspective on this solution? |
@lovebuizel, definitely your PR goes in the right direction, but without tests it's hard to say if it fixes all the cases when using For example, while I can confirm your fix solves the case when selecting days, it does not when rendering the month passing a simple export function TimeZone() {
const timeZone = "Pacific/Kiritimati";
return (
<DayPicker
defaultMonth={new Date(2024, 0, 0)} // Peru Time: -5 from GMT
mode="single"
timeZone={timeZone}
/>
);
} Here I'd expect the calendar render the January 2024, but since I'm in a timezone with ![]() I guess you couldn't see this issue because you are at +8 from GMT. To move forward, we need to add tests to make sure the whole calendar works with simple I can investigate better the issue during the next days. |
@gpbl, I agree we need to add tests for this. However, when users provide a timezone, they should be responsible for ensuring that the Date values they pass are already correctly adjusted according to the timezone they specified. This is because we cannot determine whether the Date values provided by users have already taken timezone into account or not. Therefore, we should not automatically convert the Date values to TZDate using their specified timezone. |
I think this change is OK for now! Thanks @lovebuizel - we will deal with other issues in a next update. |
What's Changed
When month passed as Date type, it would override the provided timezone settings. This fix ensures proper timezone handling.
In
react-day-picker\examples\TimeZone.tsx
, when passing a Date type for the month parameter, the timezone setting was ignored:Expected:
Actual:
After fix:

Type of Change