Fix date format to respect user profile settings using date-fns (#22900)#51526
Fix date format to respect user profile settings using date-fns (#22900)#51526ellegamee wants to merge 1 commit intohome-assistant:devfrom
Conversation
|
Have not checked the code, but imho a month should not be written as a “name”, ie “Y-M-D” should be “2026-09-23” (where leading zeroes are locale-dependent). |
Yes. The month should not be written as a "name" with the Y-M-D format. The component in the image does use a different format based upon YMD in this case. I chose to write it in this way as that's how the user settings are describing the different settings. Each card, element or text decides what format they want to display. |
93d0969 to
770189a
Compare
770189a to
c8f171d
Compare
MindFreeze
left a comment
There was a problem hiding this comment.
This loses locale-based month/weekday names. Intl.DateTimeFormat(locale.language, {...}) produced localized month and weekday names. date-fns's format defaults to English month/weekday names.
The old implementations wrapped the Intl.DateTimeFormat constructor in memoizeOne, so repeated calls with the same locale reused the same formatter. The new code creates a TZDate and re-parses the format string on every call.
|
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
Proposed change
This PR refactors the date formatting logic to ensure that the user's date format preference (DMY, MDY, or YMD) is consistently respected across the frontend.
Technical Highlights:
Standardized Pattern: Introduced a helper function formatForDatePreference that maps the user's date_format setting to specific date-fns patterns.
Improved Maintainability: Replaced custom string-building logic and redundant memoizeOne calls with a cleaner, consistent pattern using date-fns directly, as recommended by maintainers from #24935
Timezone Consistency: Extracted timezone resolution into a dedicated toTimeZone helper to ensure dates are correctly offset before formatting.
Screenshots
YMD:

MDY:

DMY:

Type of change
Additional information
Checklist
If user exposed functionality or configuration variables are added/changed:
To help with the load of incoming pull requests: