Skip to content

Commit ad12734

Browse files
fix(formatDate): fix where date is DD/MM/YYYY
1 parent 893cf90 commit ad12734

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/lib/utils.ts

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

38-
return formatter.format(new Date(date));
54+
return formatter.format(parsedDate);
3955
}
4056

4157
/**

0 commit comments

Comments
 (0)