Skip to content

Commit 69a7e98

Browse files
authored
fix(material-date-fns-adapter): locale not passed into parse function (#23653)
Fixes that the locale wasn't being passed into the parsing function of `date-fns`. Fixes #23652.
1 parent fc751ae commit 69a7e98

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/material-date-fns-adapter/adapter/date-fns-adapter.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {TestBed, waitForAsync} from '@angular/core/testing';
1010
import {DateAdapter, MAT_DATE_LOCALE} from '@angular/material/core';
1111
import {Locale} from 'date-fns';
12-
import {ja, enUS, da} from 'date-fns/locale';
12+
import {ja, enUS, da, de} from 'date-fns/locale';
1313
import {DateFnsModule} from './index';
1414

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

178+
it('should parse based on the specified locale', () => {
179+
adapter.setLocale(de);
180+
expect(adapter.parse('02.01.2017', 'P')).toEqual(new Date(2017, JAN, 2));
181+
});
182+
178183
it('should parse invalid value as invalid', () => {
179184
let d = adapter.parse('hello', 'MM/dd/yyyy');
180185
expect(d).not.toBeNull();

src/material-date-fns-adapter/adapter/date-fns-adapter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const DAY_OF_WEEK_FORMATS = {
5454
export class DateFnsAdapter extends DateAdapter<Date, Locale> {
5555
constructor(@Optional() @Inject(MAT_DATE_LOCALE) matDateLocale: {}) {
5656
super();
57-
super.setLocale(matDateLocale);
57+
this.setLocale(matDateLocale);
5858
}
5959

6060
getYear(date: Date): number {
@@ -165,7 +165,7 @@ export class DateFnsAdapter extends DateAdapter<Date, Locale> {
165165
}
166166

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

170170
if (this.isValid(fromFormat)) {
171171
return fromFormat;

0 commit comments

Comments
 (0)