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

Support Multiple Time Formats #64

Open
rohmann opened this issue Oct 10, 2022 · 1 comment
Open

Support Multiple Time Formats #64

rohmann opened this issue Oct 10, 2022 · 1 comment

Comments

@rohmann
Copy link

rohmann commented Oct 10, 2022

Angular Material allows multiple parse formats but the adapter only considers singular string values for the parseFormat.

Thought I'd mention it in case that's something that could be supported by the package internally, but was able to get around the issue by monkey patching in polyfills.ts

DateFnsAdapter.prototype.parse = function (
  value: any,
  parseFormat: string | string[]
): Date | null {
  if (value) {
    if (typeof value === 'string') {
      const parseFormats = Array.isArray(parseFormat)
        ? parseFormat
        : [parseFormat];
      const _value = value.trim();

      for (let format of parseFormats) {
        const d = parse(_value, format, new Date(), {
          locale: this._dateFnsLocale,
        });

        if (d && d.toString() !== 'Invalid Date') {
          return this.options.useUtc ? zonedTimeToUtc(d, 'UTC') : d;
        }
      }

      return new Date(value);
    }
    if (typeof value === 'number') {
      return toDate(value);
    }
    if (value instanceof Date) {
      return this.clone(value as Date);
    }
    return value;
  }
  return value;
};
@rohmann
Copy link
Author

rohmann commented Oct 12, 2022

Updated the example. Needed to stop returning null to allow form validation errors to show up since null was an acceptable value.

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

1 participant