mui / mui-x

MUI X: Build complex and data-rich applications using a growing list of advanced React components, like the Data Grid, Date and Time Pickers, Charts, and more!
https://mui.com/x/
4.08k stars 1.26k forks source link

[pickers] DateField behavior is impacted by the year format #12535

Open ash14 opened 6 months ago

ash14 commented 6 months ago

Search keywords

DateField time

Latest version

Steps to reproduce

Link to live example: https://codesandbox.io/p/sandbox/gallant-liskov-9xc4h3?file=%2Fsrc%2FApp.js%3A21%2C8-21%2C17

Steps:

  1. Enter a date like "05/05/05" in the first <DateField>
  2. Enter the same date in the second one ("05/05/2005")
  3. Note the value of the second field has a minute and hour component which is not expected

Screenshot 2024-03-22 at 5 24 46 PM

Notes:

Current behavior

When the DateField is set to use a 4-digit year format the actual value of the input has a time component. This does not occur when using a 2-digit year format.

Expected behavior

In the example above (in UTC-7) both values should display as 2005-05-05T07:00:00.000Z

Context

No response

Your environment

npx @mui/envinfo ``` Don't forget to mention which browser you used. Output from `npx @mui/envinfo` goes here. ```

Order ID or Support key 💳 (optional)

No response

LukasTy commented 6 months ago

This is a really strange behavior on dayjs side. The problem seems to stem from the fact that the year 200 is considered valid by dayjs, but when a date is created, the offset gets messed up as seen in the screenshot below. The first two lines are for the 2-digit year format The 3rd and 4th lines are for the 4-digit year format Both are in the America/New_York timezone on a device with GMT+3 timezone. Screenshot 2024-03-22 at 16 12 33 Pasting the 2005 value directly does not result in this problem.

This is just an initial report and needs further investigation.

ash14 commented 6 months ago

It seems as if some internal logic is failing to zero out hour/minute components on time zone changes across dates (comparison with zero, perhaps?)

One way to reproduce this without "invalid" years like 200 can be, e.g. when in the Asia/Hong_Kong timezone:

DavidJohnWilliams commented 1 week ago

We have a very similar issue to this.

Some additional info:

With DayJs version 1.11.7:

When typing "01/02/2022" into the picker it raises the following events:

image

Below shows the output from dayjs.isValid()

image

Code Sandbox: https://codesandbox.io/p/sandbox/datepicker-dayjs-1-11-7-ccy8nk

With DayJs version 1.11.8:

When typing "01/02/2022" into the picker it raises the following events:

image

Below shows the output from dayjs.isValid()

image

Code Sandbox: https://codesandbox.io/p/sandbox/datepicker-dayjs-1-11-13-74y97c

It seems like it could be related to https://github.com/iamkun/dayjs/commit/00c223b7e92970d07557133994fcb225a6d4c960