Repository navigation
Description
Currently the vaadin-date-picker web component converts 1-2 digit year input to the range 1950-2049 (you can try it here: https://cdn.vaadin.com/vaadin-date-picker/4.0.8/demo/#date-picker-basic-demos)
Some examples from the behaviour of the web component:
- When a year is input with 2 digits in range 0-49 is converted to 2000-2049. e.g. 19 -> 2019
- When a year is input with 2 digits in range 50-99 is converted to 1950-1999. e.g. 87 -> 1987
- When a year is input with 4 digits it is used as is. To explicitly select a date in the years 0-99 (before year 100) you need to prefix it with zeros e.g.
0012for the year 12 C.E.
This can only work when there is a way to explicitly specify a year between 0-99 (to prevent conversion) on input e.g. all years before 1000 are always formatted/displayed with 4 digits prefixing with zeros when necessary. This is also what the web component does. It always automatically formats the displayed date so that the year is always prefixed with zeros up to 4 digits.
These updates were already implemented and tested in vaadin/vaadin-date-picker-flow#230 which was not merged (in favor of a more focused fix) but the code from that PR could be reused to make a new PR just for this issues.
The fix is a bit ugly (for converting the formatted year to be prefixed with zeros) because of how the Locale support is implemented as the connector here uses Date.toLocaleDateString() which has no options to force 4 digit year output.
Imo it's not good that the behaviour is so different (when inputting a date) when just using the vaadin-date-picker web component versus using the DatePicker from Flow.
Imo, if this is fixed soon, fixing this can be considered a bug fix (since so far inputting years before 1000 didn't work at all with the Flow component until vaadin/vaadin-date-picker-flow#231 which is not yet released), but if that fix for years before 1000 is released first, then maybe this would need a new minor or even major if the default behaviour is changed.
There's also the possibility to introduce this behaviour as configurable (in both web component and Flow component, or just Flow component) so that the developer could toggle 4 digit year formatting on, and with that, optionally enable the 2 digit year conversion (possibly with configurable target range).
Activity
Haprog commentedon Apr 21, 2020
We might want to align the behaviour of the web component instead (but it would be a breaking change), see https://github.com/vaadin/vaadin-date-picker/issues/712. But even if we decide to do that, then this issue can be used for discussing/implementing the 2 digit year conversion as a new configurable feature.
[-]Convert 2 digit year input to range 1950-2049 for consistency with the web component[/-][+]Convert 1-2 digit year input to range 1950-2049 for consistency with the web component[/+]SebastianDietrich commentedon Oct 21, 2021
Just for completeness: The flow component does not allow a year smaller than 0010
And I would suggest to align the behavior of the flow component, since the web component is 1) more correct and 2) what users would expect
yuriy-fix commentedon Jan 18, 2022
We should probably use
setDateFormat/date-fnsinternally to cover this case.simasch commentedon Aug 9, 2022
What's the state of this issue?
My customer is complaining about the "misbehavior" of the DatePicker
9 remaining items
simasch commentedon Aug 12, 2022
@tomivirkki Your workaround does not work if pattern is yyyy.
@rolfsmeds great decision!
rolfsmeds commentedon Aug 19, 2022
Upon further research, the situation is actually a bit more complicated, and even less consistent than previously thought.
The custom format feature in the Flow component supports providing a list of multiple date formats for parsing (e.g.
setDateFormats("yyyy-mm-dd", "yy-mm-dd")), the first of which (the "primary" format) is also used to render the current value in the field. Parsing is attempted in the order of formats provided, so the primary format will be attempted first, and if that fails (i.e. the provided value does not match the format), the next provided format is attempted, and so forth, until it is successfully parsed.This custom formatting uses date-fns for parsing and formatting. In order to interpret "partial" date parts like 2-digit years, date-fns uses a "reference date" that the partial is compared to, so that, in the case of 2-digit years, if the year string is >50 years before the reference date, it is assumed to be in the immediate future, and if it is <50 years before the reference date, it is assumed to be in the immediate past. DatePicker currently uses the current date as the reference date.
However, it turns that
date-fnsparses a 2-digit year string with a 4-digit year format literally (e.g. "15" as year 15), rather than either using the reference date method, or failing. (There's an open issue about this in the date-fns repo.)The consequence of this is that, when the format list
"yyyy-mm-dd", "yy-mm-dd"is provided to DatePicker, parsing with"yyyy-mm-dd"is attempted first, and instead of failing so that"yy-mm-dd"is attempted next, it happily parses the 2-digit year usingyyyy, literally, so that e.g. "15" is parsed as year 15 instead of 2015.Here's an overview of the different scenarios identified, given an input string of
"50-01-01"yyyy-mm-ddyy-mm-ddyyyy-mm-dd, yy-mm-ddyyyy-mm-ddyy-mm-dd, yyyy-mm-ddSo it would be desirable to align not just the default Flow DatePicker behavior with the Web Component behavior, but also ensure that custom formats behave consistently regardless of what happens to be the primary format.
The optimal solution would probably be to align all of the above scenarios to behave based on the same reference date defaulting to the current date, ideally with an API for overriding this default reference date (this would make it possible to revert to the current behavior in applications where that is desired).
(A workaround for the non-strict date-fns parsing is provided in the linked issue: using the isMatch method to first check if the value matches the format before parsing.)Edit: Tested this and it doesn't help:
isMatchreturns true foryyyy-mm-ddand50-01-01.sissbruecker commentedon Aug 19, 2022
For custom date formats something might be salvaged by providing the option to specify the formatting pattern and the parsing patterns separately, instead of having a list for both (which makes the assumption that the formatting pattern should also be the first pattern for parsing).
rolfsmeds commentedon Aug 19, 2022
That's an option, but has 2 problems:
yy-mm-ddbeforeyyyy-mm-dd(and similar)sissbruecker commentedon Aug 19, 2022
It could be provided as an additional API, like
setDateFormats(formatPattern, parsePatterns). The current API would internally fall back to that one by passing the first entry of the list asformatPattern, and the full list asparsePatterns.Agree that it adds complexity.
tomivirkki commentedon Aug 19, 2022
What if we tried parsing with the given formats in two rounds so that the first round would use
isMatchand the second one wouldn't.setCustomFormats('yyyy-mm-dd', 'yy-mm-dd')would work as follows:isMatchfor the first pattern, so the value becomes "1950-01-01"isMatchfor the first pattern, so the second one would be used and the effective value becomes "1950-01-01"isMatchfor both patterns, so the first one is used on the second iteration.DatePicker could, under the hood, automatically add a two-digit pattern to the list if only a four-digit one is provided.
EDIT: If
isMatchdoesn't work as expected, we'd probably need to implement a similar helper ourselvesrolfsmeds commentedon Aug 19, 2022
Could a custom implementation logic be as crude as checking if the resulting year is <100, and if so, continue parsing with subsequent formats until a year>100 is achieved or you run out of formats?
rolfsmeds commentedon Oct 3, 2022
Breakdown of changes to be done in two digit year parsing:
0042, and actual year 765 is displayed as0765DatePickerI18n.setReferenceDate(LocalDate)so that the reference date can be to something other than the current date if needed.* Parsing based on reference date means that two digit years are parsed based on a reference date minus 50 years as follows:
Using reference date
2022-10-03Cutoff date:
2022-10-03minus 50 years =1972-10-03=72-10-03inyy-MM-ddEntered
yy-MM-ddstring71-10-03is before cutoff date72-10-03=> parsed as future date2071-10-03Entered
yy-MM-ddstring73-10-03is after cutoff date72-10-03=> parsed as past date1972-10-03