Skip to content

Commit 38364f3

Browse files
[pickers] Fix AdapterDayjs timezone behavior (@LukasTy) (#13373)
Signed-off-by: Lukas <[email protected]> Co-authored-by: Lukas <[email protected]>
1 parent d649cbb commit 38364f3

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

docs/data/date-pickers/timezone/timezone.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ Please check out the documentation of the [UTC and timezone on Luxon](https://mo
190190
You can then pass your UTC date to your picker:
191191

192192
```tsx
193-
import { DateTime, Settings } from 'luxon';
193+
import { DateTime } from 'luxon';
194194

195195
import { AdapterLuxon } from '@mui/x-date-pickers/AdapterLuxon';
196196
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
@@ -240,7 +240,7 @@ Please check out the documentation of the [UTC and timezone on Luxon](https://mo
240240
You can then pass your date in the wanted timezone to your picker:
241241

242242
```tsx
243-
import { DateTime, Settings } from 'luxon';
243+
import { DateTime } from 'luxon';
244244

245245
import { AdapterLuxon } from '@mui/x-date-pickers/AdapterLuxon';
246246
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';

packages/x-date-pickers/src/AdapterDayjs/AdapterDayjs.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,11 @@ export class AdapterDayjs implements MuiPickersAdapter<Dayjs, string> {
285285
if ((fixedValue.$offset ?? 0) === (value.$offset ?? 0)) {
286286
return value;
287287
}
288-
289-
return fixedValue;
288+
// Change only what is needed to avoid creating a new object with unwanted data
289+
// Especially important when used in an environment where utc or timezone dates are used only in some places
290+
// Reference: https://github.com/mui/mui-x/issues/13290
291+
// @ts-ignore
292+
value.$offset = fixedValue.$offset;
290293
}
291294

292295
return value;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as React from 'react';
2+
import dayjs from 'dayjs';
3+
import utc from 'dayjs/plugin/utc';
4+
import timezone from 'dayjs/plugin/timezone';
5+
import { DateRangePicker } from '@mui/x-date-pickers-pro/DateRangePicker';
6+
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
7+
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
8+
import { SingleInputDateRangeField } from '@mui/x-date-pickers-pro/SingleInputDateRangeField';
9+
10+
dayjs.extend(utc);
11+
dayjs.extend(timezone);
12+
13+
export default function BasicDesktopDateRangePicker() {
14+
return (
15+
<LocalizationProvider dateAdapter={AdapterDayjs}>
16+
<DateRangePicker
17+
slots={{ field: SingleInputDateRangeField }}
18+
defaultValue={[dayjs('2024-04-12'), dayjs('2024-04-14')]}
19+
/>
20+
</LocalizationProvider>
21+
);
22+
}

test/e2e/index.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,28 @@ async function initializeEnvironment(
774774

775775
expect(await page.getByRole('textbox').inputValue()).to.equal('04/11/2022 – 04/13/2022');
776776
});
777+
778+
it('should not change timezone when changing the start date from non DST to DST', async () => {
779+
// firefox in CI is not happy with this test
780+
if (browserType.name() === 'firefox') {
781+
return;
782+
}
783+
const thrownErrors: string[] = [];
784+
context.on('weberror', (webError) => {
785+
thrownErrors.push(webError.error().message);
786+
});
787+
788+
await renderFixture('DatePicker/SingleDesktopDateRangePickerWithTZ');
789+
790+
// open the picker
791+
await page.getByRole('textbox').click();
792+
793+
await page.getByRole('textbox').press('ArrowDown');
794+
795+
expect(thrownErrors).not.to.contain(
796+
'MUI X: The timezone of the start and the end date should be the same.',
797+
);
798+
});
777799
});
778800
});
779801
});

0 commit comments

Comments
 (0)