diff --git a/core/src/components/datetime/test/parse.spec.ts b/core/src/components/datetime/test/parse.spec.ts index 282ec75a42..ea2e5132b0 100644 --- a/core/src/components/datetime/test/parse.spec.ts +++ b/core/src/components/datetime/test/parse.spec.ts @@ -154,6 +154,18 @@ describe('parseMinParts()', () => { minute: 30, }); }); + it('should return undefined when given invalid info', () => { + const today = { + day: 14, + month: 3, + year: 2022, + minute: 4, + hour: 2, + }; + expect(parseMinParts(undefined, today)).toEqual(undefined); + expect(parseMinParts(null, today)).toEqual(undefined); + expect(parseMinParts('foo', today)).toEqual(undefined); + }); }); describe('parseMaxParts()', () => { @@ -205,4 +217,16 @@ describe('parseMaxParts()', () => { minute: 59, }); }); + it('should return undefined when given invalid info', () => { + const today = { + day: 14, + month: 3, + year: 2022, + minute: 4, + hour: 2, + }; + expect(parseMaxParts(undefined, today)).toEqual(undefined); + expect(parseMaxParts(null, today)).toEqual(undefined); + expect(parseMaxParts('foo', today)).toEqual(undefined); + }); }); diff --git a/core/src/components/datetime/utils/parse.ts b/core/src/components/datetime/utils/parse.ts index fda79085ac..04cd521310 100644 --- a/core/src/components/datetime/utils/parse.ts +++ b/core/src/components/datetime/utils/parse.ts @@ -132,8 +132,17 @@ export const parseAmPm = (hour: number) => { * For example, max="2012" would fill in the missing * month, day, hour, and minute information. */ -export const parseMaxParts = (max: string, todayParts: DatetimeParts): DatetimeParts => { - const { month, day, year, hour, minute } = parseDate(max); +export const parseMaxParts = (max: string, todayParts: DatetimeParts): DatetimeParts | undefined => { + const result = parseDate(max); + + /** + * If min was not a valid date then return undefined. + */ + if (result === undefined) { + return; + } + + const { month, day, year, hour, minute } = result; /** * When passing in `max` or `min`, developers @@ -168,8 +177,17 @@ export const parseMaxParts = (max: string, todayParts: DatetimeParts): DatetimeP * For example, min="2012" would fill in the missing * month, day, hour, and minute information. */ -export const parseMinParts = (min: string, todayParts: DatetimeParts): DatetimeParts => { - const { month, day, year, hour, minute } = parseDate(min); +export const parseMinParts = (min: string, todayParts: DatetimeParts): DatetimeParts | undefined => { + const result = parseDate(min); + + /** + * If min was not a valid date then return undefined. + */ + if (result === undefined) { + return; + } + + const { month, day, year, hour, minute } = result; /** * When passing in `max` or `min`, developers