Skip to content

Commit 67f5dae

Browse files
fix(utils): date error
1 parent 6cc351f commit 67f5dae

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/lib/utils.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,30 @@ export function formatDate(
2727
date: string | number | Date,
2828
options?: Intl.DateTimeFormatOptions
2929
): string {
30+
let parsedDate: Date;
31+
32+
if (typeof date === "string" && /^\d{2}\/\d{2}\/\d{4}$/.test(date)) {
33+
// If date is in DD/MM/YYYY format
34+
const [dayStr, monthStr, yearStr] = date.split("/");
35+
const day = parseInt(dayStr!, 10);
36+
const month = parseInt(monthStr!, 10);
37+
const year = parseInt(yearStr!, 10);
38+
39+
parsedDate = new Date(year, month - 1, day); // JS months are 0-based
40+
} else if (typeof date === "string" || typeof date === "number" || date instanceof Date) {
41+
parsedDate = new Date(date);
42+
} else {
43+
throw new TypeError("Invalid date format");
44+
}
45+
3046
const formatter = new Intl.DateTimeFormat(undefined, {
3147
day: "numeric",
3248
month: "short",
3349
year: "numeric",
3450
...options,
3551
});
3652

37-
return formatter.format(new Date(date));
53+
return formatter.format(parsedDate);
3854
}
3955

4056
/**

0 commit comments

Comments
 (0)