Skip to content

bug(material-date-fns-adapter): parse function does not take locale into consideration #23652

@DmitryEfimenko

Description

@DmitryEfimenko

Reproduction

Use StackBlitz to reproduce your issue:

Steps to reproduce:

  1. set MAT_DATE_LOCALE to a locale from date-fns to something other than enUS. For example: de.
  2. run the following code const parsed = dateAdapter.parse('02.01.2017', 'P')
  3. observe the value of parsed to be Date(Invalid Date) instead of an actual valid parsed date.

Expected Behavior

The value should be parsed correctly.

Actual Behavior

What behavior did you actually see?

The parsed value is Date(Invalid Date)

Proposed fix

Change line 168 in the date-fns-adapter.ts from:

const fromFormat = parse(value, currentFormat, new Date());

to

const fromFormat = parse(value, currentFormat, new Date(), { locale: this.locale });

Environment

  • Angular: 12
  • CDK/Material: next (13)
  • Browser(s): Chrome
  • Operating System: Windows

Additional notes:

The line 15 in the StackBlitz repro - if uncommented - gives an error due to a number of arguments. I tried installing the next version of @angular/core, but that didn't fix it. Not sure what to do about that, but it does not really affect the main described issue.

Metadata

Metadata

Assignees

Labels

P2The issue is important to a large percentage of users, with a workaround

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions