Skip to content

Convert 1-2 digit year input to range 1950-2049 for consistency with the web component #1107

Closed

Description

@Haprog

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. 0012 for 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

  1. Haprog commented on Apr 21, 2020

    @Haprog
    ContributorAuthor

    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.

  2. changed the title [-]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[/+] on Apr 24, 2020
  3. SebastianDietrich commented on Oct 21, 2021

    @SebastianDietrich

    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

  4. yuriy-fix commented on Jan 18, 2022

    @yuriy-fix
    Contributor

    We should probably use setDateFormat / date-fns internally to cover this case.

  5. simasch commented on Aug 9, 2022

    @simasch

    What's the state of this issue?
    My customer is complaining about the "misbehavior" of the DatePicker

  6. self-assigned this
    on Aug 10, 2022
  7. 9 remaining items

  8. simasch commented on Aug 12, 2022

    @simasch

    @tomivirkki Your workaround does not work if pattern is yyyy.

    @rolfsmeds great decision!

  9. rolfsmeds commented on Aug 19, 2022

    @rolfsmeds
    Contributor

    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-fns parses 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 using yyyy, 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"

    Scenario Resulting date Logic used
    Web Component 1950-01-01 hardcoded cutoff at "50", i.e. 49 => 2049, 50 => 1950
    Default Flow behavior 0050-01-01 literal interpretation
    Custom format yyyy-mm-dd 0050-01-01 literal interpretation
    Custom format yy-mm-dd 1950-01-01 based on current date
    Custom formats yyyy-mm-dd, yy-mm-dd 0050-01-01 literal interpretation based on yyyy-mm-dd
    Custom format yy-mm-dd, yyyy-mm-dd 1950-01-01 based on current date

    So 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: isMatch returns true for yyyy-mm-dd and 50-01-01.

  10. sissbruecker commented on Aug 19, 2022

    @sissbruecker
    Member

    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).

  11. rolfsmeds commented on Aug 19, 2022

    @rolfsmeds
    Contributor

    That's an option, but has 2 problems:

    1. Requires changing the current API, not just a slight behavior change that probably won't adversely affect many if any
    2. You still need to know that you need to provide yy-mm-dd before yyyy-mm-dd (and similar)
  12. sissbruecker commented on Aug 19, 2022

    @sissbruecker
    Member

    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 as formatPattern, and the full list as parsePatterns.

    Agree that it adds complexity.

  13. tomivirkki commented on Aug 19, 2022

    @tomivirkki
    Member

    What if we tried parsing with the given formats in two rounds so that the first round would use isMatch and the second one wouldn't.

    setCustomFormats('yyyy-mm-dd', 'yy-mm-dd') would work as follows:

    • 'yyyy-mm-dd' is used for formatting
    • "1950-01-01" would pass isMatch for the first pattern, so the value becomes "1950-01-01"
    • "50-01-01" would fail isMatch for the first pattern, so the second one would be used and the effective value becomes "1950-01-01"
    • "5-01-01" would fail isMatch for 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 isMatch doesn't work as expected, we'd probably need to implement a similar helper ourselves

  14. rolfsmeds commented on Aug 19, 2022

    @rolfsmeds
    Contributor

    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?

  15. rolfsmeds commented on Oct 3, 2022

    @rolfsmeds
    Contributor

    Breakdown of changes to be done in two digit year parsing:

    • Change (non-Flow) vaadin-date-picker default parsing to select past/future based on reference date* instead of hardcoded 1950.
    • Change Flow DatePicker default parsing to select past/future based on reference date*.
    • Change Flow DatePicker custom date format based parsing to select past/future based on reference date*.
    • vaadin-date-picker and Flow DatePicker: if a 4 digit presentation format is used, pad years 0–999 with leading zeros, so that e.g. actual year 42 is displayed as 0042, and actual year 765 is displayed as 0765
    • *Reference date defaults to current date.
    • Add API to DatePickerI18n for setting & getting the reference date, e.g. DatePickerI18n.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-03
    Cutoff date: 2022-10-03 minus 50 years = 1972-10-03 = 72-10-03 in yy-MM-dd
    Entered yy-MM-dd string 71-10-03 is before cutoff date 72-10-03 => parsed as future date 2071-10-03
    Entered yy-MM-dd string 73-10-03is after cutoff date 72-10-03 => parsed as past date 1972-10-03

  16. self-assigned this
    on Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions