Skip to content

Commit

Permalink
fix: handle date foramting types
Browse files Browse the repository at this point in the history
  • Loading branch information
Vojimirovich committed Nov 7, 2024
1 parent 932f803 commit 8cc6d24
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions frontend/src/lib/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ import { format, parseISO } from "date-fns";
* formatDisplayDate("invalid date");
* // Returns "Invalid date"
*/
export function formatDisplayDate(dateString: string): string {
if (!dateString) {
return "Invalid date";
}
export function formatDisplayDate(date: string | Date): string {
let dateToFormat: Date;

try {
const date = parseISO(dateString);
return format(date, "do LLL yyyy");
} catch (error) {
console.error("Error parsing date:", error);
if (typeof date === "string") {
try {
dateToFormat = parseISO(date);
} catch (error) {
return "Invalid date";
}
} else if (date instanceof Date) {
dateToFormat = date;
} else {
return "Invalid date";
}

return format(dateToFormat, "do LLL yyyy");
}

0 comments on commit 8cc6d24

Please sign in to comment.