Skip to content

fix(material/datepicker): fix handling of short years #20709

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

Merged
merged 1 commit into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/material/core/datetime/native-date-adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe('NativeDateAdapter', () => {
expect(adapter.getYearName(new Date(2017, JAN, 1))).toBe('2017');
});

it('should year name for low year numbers', () => {
it('should get year name for low year numbers', () => {
const createAndFormat = (year: number) => {
return adapter.getYearName(adapter.createDate(year, JAN, 1));
};
Expand Down
30 changes: 12 additions & 18 deletions src/material/core/datetime/native-date-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,12 @@ export class NativeDateAdapter extends DateAdapter<Date> {

/** Creates a date but allows the month and date to overflow. */
private _createDateWithOverflow(year: number, month: number, date: number) {
return this._correctYear(new Date(year, month, date), year);
// Passing the year to the constructor causes year numbers <100 to be converted to 19xx.
// To work around this we use `setFullYear` and `setHours` instead.
const d = new Date();
d.setFullYear(year, month, date);
d.setHours(0, 0, 0, 0);
return d;
}

/**
Expand Down Expand Up @@ -318,22 +323,11 @@ export class NativeDateAdapter extends DateAdapter<Date> {
* @returns A Date object with its UTC representation based on the passed in date info
*/
private _format(dtf: Intl.DateTimeFormat, date: Date) {
const year = date.getFullYear();
const d = new Date(Date.UTC(
year, date.getMonth(), date.getDate(), date.getHours(),
date.getMinutes(), date.getSeconds(), date.getMilliseconds()));
return dtf.format(this._correctYear(d, year));
}

/**
* Corrects the year of a date, accounting for the fact that JS
* native Date treats years between 0 and 99 as abbreviations for 19xx.
*/
private _correctYear(date: Date, intendedYear: number): Date {
if (intendedYear >= 0 && intendedYear < 100) {
date.setFullYear(this.getYear(date) - 1900);
}

return date;
// Passing the year to the constructor causes year numbers <100 to be converted to 19xx.
// To work around this we use `setUTCFullYear` and `setUTCHours` instead.
const d = new Date();
d.setUTCFullYear(date.getFullYear(), date.getMonth(), date.getDate());
d.setUTCHours(date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
return dtf.format(d);
}
}