Skip to content

Commit

Permalink
fix(material-date-fns-adapter): locale not passed into parse function (
Browse files Browse the repository at this point in the history
…#23653)

Fixes that the locale wasn't being passed into the parsing function of `date-fns`.

Fixes #23652.

(cherry picked from commit 69a7e98)
  • Loading branch information
crisbeto authored and zarend committed Oct 12, 2021
1 parent 1b8c586 commit 2a35812
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import {TestBed, waitForAsync} from '@angular/core/testing';
import {DateAdapter, MAT_DATE_LOCALE} from '@angular/material/core';
import {Locale} from 'date-fns';
import {ja, enUS, da} from 'date-fns/locale';
import {ja, enUS, da, de} from 'date-fns/locale';
import {DateFnsModule} from './index';

const JAN = 0, FEB = 1, MAR = 2, DEC = 11;
Expand Down Expand Up @@ -175,6 +175,11 @@ describe('DateFnsAdapter', () => {
expect(adapter.parse('', 'MM/dd/yyyy')).toBeNull();
});

it('should parse based on the specified locale', () => {
adapter.setLocale(de);
expect(adapter.parse('02.01.2017', 'P')).toEqual(new Date(2017, JAN, 2));
});

it('should parse invalid value as invalid', () => {
let d = adapter.parse('hello', 'MM/dd/yyyy');
expect(d).not.toBeNull();
Expand Down
4 changes: 2 additions & 2 deletions src/material-date-fns-adapter/adapter/date-fns-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const DAY_OF_WEEK_FORMATS = {
export class DateFnsAdapter extends DateAdapter<Date, Locale> {
constructor(@Optional() @Inject(MAT_DATE_LOCALE) matDateLocale: {}) {
super();
super.setLocale(matDateLocale);
this.setLocale(matDateLocale);
}

getYear(date: Date): number {
Expand Down Expand Up @@ -165,7 +165,7 @@ export class DateFnsAdapter extends DateAdapter<Date, Locale> {
}

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

if (this.isValid(fromFormat)) {
return fromFormat;
Expand Down

0 comments on commit 2a35812

Please sign in to comment.