File tree Expand file tree Collapse file tree 1 file changed +18
-2
lines changed
Expand file tree Collapse file tree 1 file changed +18
-2
lines changed Original file line number Diff line number Diff line change @@ -26,16 +26,32 @@ export function bytesToReadable(bytes: number): string {
2626 */
2727export 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/**
You can’t perform that action at this time.
0 commit comments