fix(datetime-button): render correct text when passing partial date values#27816
Conversation
|
|
| * account for this and still return a valid date. However, | ||
| * this is not a consistent behavior across all browsers. | ||
| */ | ||
| return new Date(`${refParts.month ?? 1}/${refParts.day ?? 1}/${refParts.year ?? 2023}${timeString} GMT+0000`); |
There was a problem hiding this comment.
Should we fallback to the current year, instead of hardcoding 2023?
e.g.:
refParts.year ?? new Date().getFullYear()There was a problem hiding this comment.
We could, though we also default to 1/1/2023 here:
Any preference?
There was a problem hiding this comment.
I don't have a preference myself. Presumably, if I'm passing in a value without the year, I'm using the month presentation, in which case I don't care about the year since it'll never be displayed.
That said, if we do go with the current year, I would rather use the current date for the month and day as well for consistency's sake.
There was a problem hiding this comment.
Presumably, if I'm passing in a value without the year, I'm using the month presentation, in which case I don't care about the year since it'll never be displayed.
Yep exactly this. If any of those values are undefined then they aren't actually used in the rendering of the component -- we just need them to get a valid date
There was a problem hiding this comment.
I'm ok with this as-is then 👍
Issue number: resolves #27797
What is the current behavior?
Datetime Button passes a parsed value to one of the many text formatting utilities we have, such as
getMonthAndYear. However, developers can pass partial date values such as2022or2022-04(April 2022). According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_time_string_format, these are still valid date strings.However, the
parseDateutility does not add fallback values. So passing2022will cause thedayandmonthfields to beundefined. This means thatgetNormalizedDatepasses'//2022'to theDateconstructor.Some browsers, such as Chrome, will automatically account for the stray slashes and still return a valid date. Other browsers, such as Safari, do not do this and will either return "Invalid Date" or throw an error.
What is the new behavior?
getNormalizedDatewill now pass'1/1/2022'instead of'//2022'to theDateconstructor.new Dateto make use ofgetNormalizedDatesince they are also impacted.Note: I added an E2E test instead of a spec test because I want to test cross-browser behavior to ensure consistency.
Does this introduce a breaking change?
Other information