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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pickers] How to set a global timezone on the pickers with dayjs #6948

Closed
2 tasks done
d-s-j opened this issue Nov 22, 2022 · 3 comments 路 Fixed by #7610
Closed
2 tasks done

[pickers] How to set a global timezone on the pickers with dayjs #6948

d-s-j opened this issue Nov 22, 2022 · 3 comments 路 Fixed by #7610
Assignees
Labels
bug 馃悰 Something doesn't work component: pickers This is the name of the generic UI component, not the React module! support: commercial Support request from paid users

Comments

@d-s-j
Copy link

d-s-j commented Nov 22, 2022

Order ID 馃挸

50497

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

The problem in depth 馃攳

Hi there 馃憢 My team and I are currently trying to set a global timezone (UTC 00:00) for our entire React application. This is due to how the backend handles and serves dates: no times are included and is assumed to be UTC 00:00.

The expectation would be that regardless of your local timezone, a user interacting with the DateRangePicker from Tokyo should see the same highlighted today date as a user in San Francisco, as well as any dates selected.

Current behaviour viewing from San Francisco:
image

Current behaviour viewing from Tokyo:
Screenshot 2022-11-21 at 4 30 33 PM

I've provided a codebox demo of roughly how we're attempting to accomplish this, using the LocalizationProvider and passing dayjs.utc as the instance, which I understand is what the DateRangePicker will base it's dates from, but this raises the TypeError: Cannot read properties of undefined (reading 'en'). Any suggestions/feedback would be appreciated 馃檹

Your environment 馃寧

Codesandbox Demo

@d-s-j d-s-j added status: waiting for maintainer These issues haven't been looked at yet by a maintainer support: commercial Support request from paid users labels Nov 22, 2022
@flaviendelangle
Copy link
Member

Hi,

The problem in your code is that dayjs.utc is not a valid dayjs instance.
That's sad because otherwise your approach would probably solve the UTC problem perfectly...

You can pass UTC dates in your initial value, and the components should not loose the UTC aspect when updating them (see this example).

But if you want to pass no initial date (which is of course a very valid use case), then our current API seem to be missing something...
The following hack seems to work.
I will try to see if we are able to somehow get the locale information (what is inside dayjs.Ls) from the UTC instance on our side 馃憤

@flaviendelangle flaviendelangle added bug 馃悰 Something doesn't work component: pickers This is the name of the generic UI component, not the React module! and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Nov 22, 2022
@flaviendelangle flaviendelangle changed the title How to set a global timezone for the DateRangePicker [pickers] How to set a global timezone with dayjs Nov 22, 2022
@flaviendelangle flaviendelangle changed the title [pickers] How to set a global timezone with dayjs [pickers] How to set a global timezone on the pickers with dayjs Nov 22, 2022
@flaviendelangle
Copy link
Member

flaviendelangle commented Nov 22, 2022

Looking at the UTC plugin code, it seems that we can't get the dayjs main instance from it.
But maybe we don't need to use the custom dayjs instance passed to dateLibInstance when we try to get the localized formats.

The following diff should fix the problem, but we need to take a deeper look in case it breaks something:

--- a/packages/x-date-pickers/src/AdapterDayjs/index.ts
+++ b/packages/x-date-pickers/src/AdapterDayjs/index.ts
@@ -1,4 +1,4 @@
-import { Dayjs } from 'dayjs';
+import dayjs, { Dayjs } from 'dayjs';
 import BaseAdapterDayjs from '@date-io/dayjs';
 import { MuiFormatTokenMap, MuiPickerFieldAdapter } from '../internals/models';
 import { buildWarning } from '../internals/utils/warning';
@@ -39,13 +39,12 @@ export class AdapterDayjs extends BaseAdapterDayjs implements MuiPickerFieldAdap
    * We should use this one in the future to support all localized formats.
    */
   public expandFormat = (format: string) => {
-    const localeObject = this.rawDayJsInstance.Ls[this.locale || 'en'];
+    const localeObject = dayjs.Ls[this.locale || 'en'];
 
     if (localeObject === undefined) {
       localeNotFoundWarning();
     }
-    const localeFormats =
-      localeObject === undefined ? this.rawDayJsInstance.Ls.en.formats : localeObject.formats;
+    const localeFormats = localeObject === undefined ? dayjs.Ls.en.formats : localeObject.formats;
 
     // @see https://github.com/iamkun/dayjs/blob/dev/src/plugin/localizedFormat/index.js
     const t = (formatBis: string) =>

Side note: Ths dateLibInstance approach will probably not work for dayjs.tz, we need to rework our timezone approach because it is very bad...

@Semigradsky
Copy link

I have the same issue (incorrect today highlight) and I 'fixed' it by overriding renderDay for passing my calculated today value to PickersDay. But it doesn't work for month/year because we can't customize them yet - #4662

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 馃悰 Something doesn't work component: pickers This is the name of the generic UI component, not the React module! support: commercial Support request from paid users
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants