bug(DateAdapter): Date comparison is only checking partial info #28726
Replies: 3 comments 1 reply
-
I believe this is intentional. We only check that the year, month and day match, because the datepicker only selects dates and not dates + times. |
Beta Was this translation helpful? Give feedback.
-
It's actually not only about the time that is ignored but everything that is stored in the date instance that adapter provides. For example luxon's In my case my initial value for the Since the And the fix for this is straightforward if the custom adapters simply (and optionally) override the |
Beta Was this translation helpful? Give feedback.
-
Hello @mpflanzer , Thank you for the detailed information.
I don't think MatDatepicker has any requirement to work with time input or support time at all. I think the recommended solution might be for you to make your own adapter for your specific use case. I'm going to convert this to discussion. That way this issue doesn't get locked, and you will still be able to post comments. -Zach |
Beta Was this translation helpful? Give feedback.
-
Is this a regression?
The previous version in which this bug was not present was
No response
Description
I'm developing an app with Material DatePicker and Luxon Date Adapter.
I gave my users the possibility to input seconds, milliseconds or full ISO date strings into my app's date inputs.
This works well except when pasting certain dates without deleting the input's value first (replacing value with CTRL+V).
I found out why, I was pasting dates which were on the same day (but different time values) as the previous date.
Video of the problem:
https://github.com/angular/components/assets/15314924/f19fc5e0-f019-474c-b854-85a177b86703
Checking the source code reveals that in the date-adapter.ts file, dates are only compared using year/month/day instead of relying on the native's type when applicable.
components/src/material/core/datetime/date-adapter.ts
Lines 243 to 249 in facd027
And the method sameDate() is called by MatDatePickerInputBase, and its result is based on the previous method's result:
components/src/material/core/datetime/date-adapter.ts
Lines 258 to 268 in facd027
Neither method is overriden in luxon-date-adapter to use something more robust.
Using my own date adapter worked as expected with this snippet:
This fixed the problem for Luxon but for any other adapter not overloading this method (including native dates) the problem arises.
The DateAdapter interface and class does not account for milliseconds (there is no dateAdapter.getMillis), perhaps it should ?
Reproduction
I have a demo app of a lib I'm managing having the problem :
https://thomasprioul.github.io/mdl-angular-libs/#/home/forms
Steps to reproduce:
Expected Behavior
Correct detection of "different date" on CTRL+V
Actual Behavior
New input is considered as "equal date" when it is not
Environment
Beta Was this translation helpful? Give feedback.
All reactions