Support for different timezones #1415
Replies: 18 comments 14 replies
-
Hey thanks for opening the issue. Please forget the old one because we've lost the thread there...
Wouldn't be easier to set the date to the user's timezone instead? Why setting a date using another offset? Help us understand with an example using the beta: https://codesandbox.io/s/ivs35 thanks. |
Beta Was this translation helpful? Give feedback.
-
@gpbl That's just a feature that our app supports, the user can configure the way the dates are displayed in the entire app based on the timezone they picked So we can't just show the date in their own timezone. For example if one of our clients have offices in Japan and in the US, that client has an option to show the date for everyone in UTC. |
Beta Was this translation helpful? Give feedback.
-
Sorry, I fixed the codebox link. Say we do support this feature, maybe with a Timezones are still confusing to me :) I understand that date-fns is not supporting timezones but date-fns-tz does. I'd prefer to not add a new dependency to this package so I'm trying to understand if this can be done from the consumer side. |
Beta Was this translation helpful? Give feedback.
-
Hey @gpbl, sorry for the delay. Here's an example of what I'm talking about: https://codesandbox.io/s/sandpack-project-forked-dd9bh So I'm using in the component a date that's in UTC That happens because the <DayPicker timezone="UTC" /> |
Beta Was this translation helpful? Give feedback.
-
@mauriciosoares let me understand the need to initialize the date by string and not by I think I understand the requirement, I wonder for now if there's an acceptable workaround without changing DayPicker, that we could add to the documentation. |
Beta Was this translation helpful? Give feedback.
-
The |
Beta Was this translation helpful? Give feedback.
-
We discovered parseISO from date-fns to solve this problem |
Beta Was this translation helpful? Give feedback.
-
So is there a plan to include that in the library in a future release? If so, do you think there's a way to workaround that for now? |
Beta Was this translation helpful? Give feedback.
-
Here is advised we should not: https://developer.mozilla.org/enUS/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
I think the suggestion by @tjfred35 is to use I see this as a non-issue, or an improvement to the developer experience. Sure we should add a note to the documentation, maybe even some dev-only alert when passing dates with a different timezone. I wonder however how does other libraries like |
Beta Was this translation helpful? Give feedback.
-
It seems a common request, but the solution proposed is always to not pass UTC dates: Hacker0x01/react-datepicker#1787 The date-fns-tz package may help creating a wrapper over DayPicker. For example, we could create a new It would be nice if somebody familiar with the react-day-picker source code to explore the alternatives here, the amount of work, etc. |
Beta Was this translation helpful? Give feedback.
-
This branch https://github.com/gpbl/react-day-picker/tree/utc has added a Also related discussion: #1149 |
Beta Was this translation helpful? Give feedback.
-
I have also the same issue, very problematic. Could we have the choice to use TZ or not? |
Beta Was this translation helpful? Give feedback.
-
Seems like this did a trick for me: export function correctUTC(date: Date) {
return new Date(date.getTime() - date.getTimezoneOffset() * 60000);
} Just use it <DayPicker
mode="single"
selected={field.value}
onSelect={(selectDate) =>
field.onChange(correctUTC(selectDate))
}
/> Doesn't matter what timezone I set, I get the same datetime |
Beta Was this translation helpful? Give feedback.
-
Should note here the recent release of https://github.com/date-fns/utc. Could this library help with supporting UTC in Day Picker? |
Beta Was this translation helpful? Give feedback.
-
I've been recently trying to upgrade from v7 and experiencing a lot of the same issues. Handling different time zones and selected/picked dates being off by a day. One of the things I ended up having to do is convert all dates to a UTC standard. The standard date string would be /**
* Converts a string to a Date object with the current local timestamp.
* @param date - string
* @param dateFormat - format of date string, default = 'yyyy-MM-dd'
* @returns UTCDate
*/
export const getTimeStamp = (date: string, dateFormat = 'yyyy-MM-dd'): Date => {
const now = new Date();
const day = parse(date, dateFormat, now);
return new Date(
day.getUTCFullYear(),
day.getUTCMonth(),
day.getUTCDate(),
now.getUTCHours() - now.getTimezoneOffset() / 60,
now.getUTCMinutes()
);
}; This will likely need to change as I'd really like to see these issues addressed in v9 of day picker. |
Beta Was this translation helpful? Give feedback.
-
Hi here, in v9 ( https://daypicker.dev/next/using-daypicker/localization#utc-dates Please let me know if this solution works for you. Having UTC, is supporting different timezones still useful? Looking for some use cases. |
Beta Was this translation helpful? Give feedback.
-
It looks like with the new We can publish this prop with an experimental flag to see how it goes. I'd love to know the feedback from participants here 🙏🏽. |
Beta Was this translation helpful? Give feedback.
-
The new experimental import { DayPicker, TZDate } from 'react-day-picker';
<DayPicker
timeZone="Europe/Athens" // set the time zone
disabled={TZDate.tz("Europe/Athens")} // make sure you use `TZDate` to initialize dates
/> Please open issues or another discussion if you would like to provide feedbacks. Thanks! |
Beta Was this translation helpful? Give feedback.
-
Is your feature request related to a problem? Please describe.
This is a feature that was requested a while back here: #1149, a few devs are having problems when their apps deals with different timezones
Describe the solution you'd like
We could use a prop like
timezone
where we can set what's the timezone that the calendar should consider for setting the date.Today if we set a date that has an UTC offset of 0, let's say
Thu, 27 Jan 2022 00:00:00 GMT-0000
, if the user's browser timezone is set to something with a negative UTC offset (like BRT which is -3), then when opening the date picker, the picker will have selected 26 of January, becauseThu, 27 Jan 2022 00:00:00 GMT-0000
in BRT isWed Jan 26 2022 21:00:00 GMT-0300
.So the picker, if configured, shouldn't consider the browsers current timezone, it should consider the one configured as a prop.
Beta Was this translation helpful? Give feedback.
All reactions