diff --git a/src/lib/intlFormat.test.ts b/src/lib/intlFormat.test.ts index f204d96a5..a94e4b0f1 100644 --- a/src/lib/intlFormat.test.ts +++ b/src/lib/intlFormat.test.ts @@ -185,6 +185,14 @@ describe('intlFormat', () => { expect(date).toBeNull(); }); + + it('returns if month is null', () => { + const date = dateFromParts(0, 0, 2000, locale); + + expect(date).toBe( + 'Invalid Date - you specified 0 (of type number) as a month, which is invalid', + ); + }); }); //this test often fails locally. It passes on github. describe('dateTimeFormat', () => { diff --git a/src/lib/intlFormat.ts b/src/lib/intlFormat.ts index cb9dafaa0..8b19e27b2 100644 --- a/src/lib/intlFormat.ts +++ b/src/lib/intlFormat.ts @@ -89,8 +89,16 @@ export const dateFromParts = ( } if (typeof year === 'number') { - return dateFormat(DateTime.local(year, month, day), locale); + const date = DateTime.local(year, month, day); + if (date.invalidReason || date.invalidExplanation) { + return `Invalid Date - ${date.invalidExplanation}`; + } + return dateFormat(date, locale); } else { + const date = DateTime.local().set({ month, day }); + if (date.invalidReason || date.invalidExplanation) { + return `Invalid Date - ${date.invalidExplanation}`; + } return dayMonthFormat(day, month, locale); } };