From 64f3bd94d741fd7226c4b79ac41842949bcdb16f Mon Sep 17 00:00:00 2001 From: Daniel Bisgrove Date: Wed, 19 Jun 2024 13:33:10 -0400 Subject: [PATCH] Return "invalid date" when date is not formatted correctly. Stopping the blank screen error --- src/lib/intlFormat.test.ts | 8 ++++++++ src/lib/intlFormat.ts | 10 +++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) 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); } };