File tree Expand file tree Collapse file tree 1 file changed +17
-1
lines changed
Expand file tree Collapse file tree 1 file changed +17
-1
lines changed Original file line number Diff line number Diff 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/**
You can’t perform that action at this time.
0 commit comments