Skip to content

Commit 6a8f549

Browse files
committed
Simplify date component validity check
1 parent edf9d2d commit 6a8f549

File tree

1 file changed

+9
-25
lines changed

1 file changed

+9
-25
lines changed

src/material/core/datetime/native-date-adapter.ts

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export class NativeDateAdapter extends DateAdapter<Date> {
146146
return new Date(value);
147147
}
148148

149-
const dateParts = (value as string)
149+
const dateParts = value
150150
.trim()
151151
.split(DATE_COMPONENT_SEPARATOR_REGEX)
152152
.map(part => parseInt(part, 10))
@@ -191,13 +191,14 @@ export class NativeDateAdapter extends DateAdapter<Date> {
191191
}
192192
}
193193

194-
if (
195-
year !== null &&
196-
month !== null &&
197-
day !== null &&
198-
this._dateComponentsAreValid(year, month, day)
199-
) {
200-
return this.createDate(year, month, day);
194+
if (year !== null && month !== null && day !== null) {
195+
const date = this.createDate(year, month, day);
196+
197+
if (date.getFullYear() === year && date.getMonth() === month && date.getDate() === day) {
198+
return date;
199+
}
200+
201+
return this.invalid();
201202
}
202203

203204
return this._nativeParseFallback(value);
@@ -348,21 +349,4 @@ export class NativeDateAdapter extends DateAdapter<Date> {
348349
date.getUTCMilliseconds(),
349350
);
350351
}
351-
352-
private _dateComponentsAreValid(year: number, month: number, day: number) {
353-
if (year < 0 || year > 9999 || month < 0 || month > 11 || day < 1 || day > 31) {
354-
return false;
355-
}
356-
357-
if (month === 1) {
358-
const isLeapYear = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
359-
return isLeapYear ? day <= 29 : day <= 28;
360-
}
361-
362-
if (month === 3 || month === 5 || month === 8 || month === 10) {
363-
return day <= 30;
364-
}
365-
366-
return true;
367-
}
368352
}

0 commit comments

Comments
 (0)