-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[pickerts] not correctly setting the date time using dayjs with timezone #11955
Comments
I encountered the same issue 😔 |
Note that I think I fixed this issue on my codebase by doing something like export const CustomDesktopDatePicker = () => {
const [lastDate, setLastDate] = useState(null)
return (
<DesktopDatePicker
onChange={(date) => {
const nativeDate = date?.toDate()
if (!nativeDate) {
return
}
if (
lastDate &&
nativeDate.getTimezoneOffset() !==
lastDate.getTimezoneOffset()
) {
nativeDate.setHours(0, 0, 0, 0)
}
if (dayjs(nativeDate).isValid()) setLastDate(nativeDate)
}}
/>
)
} |
The problem is that the introduction of UTC and timezones is faulty in Dayjs. import { useState } from 'react';
import { Moment } from 'moment';
import { DesktopDatePicker } from '@mui/x-date-pickers/DesktopDatePicker';
import { AdapterMoment } from '@mui/x-date-pickers/AdapterMoment';
import { LocalizationProvider } from '@mui/x-date-pickers';
const CompTest = () => {
const [value, setValue] = useState<Moment | null>(null);
return (
<LocalizationProvider dateAdapter={AdapterMoment} adapterLocale={'de'}>
<DesktopDatePicker
value={value}
onChange={(value) => {
setValue(value);
}}
/>
<p>Date: {value?.toString()}</p>
</LocalizationProvider>
);
};
export default CompTest; There are open issues on the dayjs packages (and even open PRs - one from me), but the maintainer seems to be inactive. Example PR EDIT Example with Luxon below: #11955 (comment) |
Would using |
My apologies ... I was not aware that it is in maintenance mode. I just picked it because I am the most familiar with this package! :D import { useState } from 'react';
import { DateTime } from 'luxon';
import { DesktopDatePicker } from '@mui/x-date-pickers/DesktopDatePicker';
import { AdapterLuxon } from '@mui/x-date-pickers/AdapterLuxon';
import { LocalizationProvider } from '@mui/x-date-pickers';
const CompTest = () => {
const [value, setValue] = useState<DateTime | null>(DateTime.utc(2024, 1, 1, 0, 0, 0, 0));
return (
<LocalizationProvider dateAdapter={AdapterLuxon} adapterLocale={'de'}>
<DesktopDatePicker
value={value}
onChange={(value) => {
setValue(value);
}}
/>
<p>Date: {value?.toString()}</p>
</LocalizationProvider>
);
};
export default CompTest; |
Thanks for your replies! Unfortunately, the project I am working on recently made the switch from moment to dayjs, and I can't make the switch to Luxon, because of the differences in API between Luxon and Dayjs. |
Good to hear that your solution for this is working for you. 👍🏼 |
I will close this issue for now, but feel free to reopen if you have any further questions regarding this topic! |
Steps to reproduce
Link to live example: https://codesandbox.io/p/sandbox/miu-datepicker-bug-date-5npl6g
Steps:
Current behavior
The date returned by the DesktopDatePicker has a time, depending of the timezone.
Expected behavior
The time returned by the DesktopDatePicker should be similar as the one returned when there is no timezone involved (i.e. "00:00:00"), but relative to the timezone.
For example, if the inputed date is
01/01/2020
(in MM/DD/YYYY format) in a GMT+0100 environment, it should return the date2021-12-31T23:00:00.000Z
Context
No response
Your environment
npx @mui/envinfo
Search keywords: dayjs timezone datepicker
The text was updated successfully, but these errors were encountered: