Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IUtils<TDate>.date method should be more locale aware #668

Open
RafalOsieka opened this issue May 24, 2024 · 3 comments
Open

IUtils<TDate>.date method should be more locale aware #668

RafalOsieka opened this issue May 24, 2024 · 3 comments

Comments

@RafalOsieka
Copy link

Hi,

I was investigating the issue in different UI library that uses the date-io to abstract the date/time functionality (vuetifyjs/vuetify#19803).

It seems, that authors uses the adapter.date(...) (where adapter is the selected implementation of the time management lib).

Unfortunatelly, that method seems, to only care for the date provided in the format mm/dd/yyyy.

Let's assume the following example:

  • we have an input that allows to provide the date (as a string) in a specific format (depending of selected UI locale - en-us (mm/dd/yyyy) or polish (dd.mm.yyyy)
  • some code will validate that value depending on the current locale
  • if adapter.date(...) is used for the validation, then everything is OK if en-us is selected, but if other locale is selected (like pl), then the value inputed for the adapter.date(...) method should be validated against the pl locale format, not en

I think, that it will be just enough to extend the date method with some additional checks. The following is an example of the new date method implemenation for the date-fns interface, that should fix the issue (at least in a very common scenario).

import DateFnsAdapter from '@date-io/date-fns';
import { isValid as dateFnsIsValid } from 'date-fns';

class BetterDateFnsAdapter extends DateFnsAdapter {
  date(value) {
    if (typeof value === "undefined") return new Date();
    if (value === null) return null;

    const defaultParsedValue = super.date(value);
    if (dateFnsIsValid(defaultParsedValue)) return defaultParsedValue;

    try {
      let parseResult = this.parse(value, this.formats.keyboardDateTime24h);
      if (dateFnsIsValid(parseResult)) return parseResult;

      parseResult = this.parse(value, this.formats.keyboardDateTime);
      if (dateFnsIsValid(parseResult)) return parseResult;

      parseResult = this.parse(value, this.formats.keyboardDate);
      if (dateFnsIsValid(parseResult)) return parseResult;
    } catch {
      // ignore
    }

    return defaultParsedValue;
  }
}
@dmtrKovalenko
Copy link
Owner

The intention of the date method is not to parse the input but to create the date object from the most basic values like js date and iso string.

There are separate methods implemented with locale specific and format specific parsing.

@RafalOsieka
Copy link
Author

In that case, the only option is to use the parse method like it was used in the example I provided.

Unfortunately, it requires to provide a format string, so it will be the job of the consumers of this library to make correct implementation for that.

Maybe it would be better to provide a new method in this library for that use case? For example, a new parse method that doesn't need an format string, but just tries to parse the provided value using the current locale?

@dmtrKovalenko
Copy link
Owner

It is by design restriction to avoid consumers of the library from the parsing of arbitary date strings. The date string that you are parsing must be always strictly formatted and provided for the users (thats why we exposer getFormatHelperText method)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants