Skip to content

Commit

Permalink
Return "invalid date" when date is not formatted correctly. Stopping …
Browse files Browse the repository at this point in the history
…the blank screen error
  • Loading branch information
dr-bizz committed Jun 19, 2024
1 parent deafe90 commit 64f3bd9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/lib/intlFormat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
10 changes: 9 additions & 1 deletion src/lib/intlFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;

Check warning on line 100 in src/lib/intlFormat.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/intlFormat.ts#L100

Added line #L100 was not covered by tests
}
return dayMonthFormat(day, month, locale);
}
};
Expand Down

0 comments on commit 64f3bd9

Please sign in to comment.