Skip to content

Commit 2285cfa

Browse files
committed
fix(datepicker): year not formatted in multi-year view button
Fixes the years that are rendered in the calendar's perioud button not being formatted through the date adapter in the multi-view. This can result in an incorrect value being displayed in some locales. Fixes #17187.
1 parent 7cd78ff commit 2285cfa

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/material/datepicker/calendar-header.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Directionality} from '@angular/cdk/bidi';
22
import {Component} from '@angular/core';
33
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
4-
import {MatNativeDateModule} from '@angular/material/core';
4+
import {MatNativeDateModule, DateAdapter} from '@angular/material/core';
55
import {DEC, FEB, JAN} from '@angular/material/testing';
66
import {By} from '@angular/platform-browser';
77
import {MatCalendar} from './calendar';
@@ -149,6 +149,18 @@ describe('MatCalendarHeader', () => {
149149
expect(calendarInstance.activeDate).toEqual(new Date(2016, DEC, 31));
150150
expect(testComponent.selected).toBeFalsy('no date should be selected yet');
151151
});
152+
153+
it('should format the year in the period button using the date adapter', () => {
154+
const adapter = fixture.debugElement.injector.get(DateAdapter);
155+
156+
spyOn(adapter, 'getYearName').and.returnValue('FAKE_YEAR');
157+
158+
periodButton.click();
159+
fixture.detectChanges();
160+
161+
expect(calendarInstance.currentView).toBe('multi-year');
162+
expect(periodButton.textContent).toContain('FAKE_YEAR');
163+
});
152164
});
153165

154166
describe('calendar with minDate only', () => {

src/material/datepicker/calendar.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ export class MatCalendarHeader<D> {
8282
const minYearOfPage = activeYear - getActiveOffset(
8383
this._dateAdapter, this.calendar.activeDate, this.calendar.minDate, this.calendar.maxDate);
8484
const maxYearOfPage = minYearOfPage + yearsPerPage - 1;
85-
return `${minYearOfPage} \u2013 ${maxYearOfPage}`;
85+
const minYearName =
86+
this._dateAdapter.getYearName(this._dateAdapter.createDate(minYearOfPage, 0, 1));
87+
const maxYearName =
88+
this._dateAdapter.getYearName(this._dateAdapter.createDate(maxYearOfPage, 0, 1));
89+
return `${minYearName} \u2013 ${maxYearName}`;
8690
}
8791

8892
get periodButtonLabel(): string {

0 commit comments

Comments
 (0)