@@ -3,25 +3,36 @@ export function formatDate(value, format = 'datetime') {
3
3
const iso8601Regex = / ^ \d { 4 } - \d { 2 } - \d { 2 } T \d { 2 } : \d { 2 } : \d { 2 } .\d { 3 } Z $ / ;
4
4
// Regex for simple YYYY-MM-DD format
5
5
const simpleDateRegex = / ^ \d { 4 } - \d { 2 } - \d { 2 } $ / ;
6
+ // Regex for long-form date strings like 'Wed Dec 11 2024 14:46:19 GMT+0200'
7
+ const longFormDateRegex = / ^ [ A - Z ] [ a - z ] { 2 } [ A - Z ] [ a - z ] { 2 } \d { 2 } \d { 4 } \d { 2 } : \d { 2 } : \d { 2 } G M T [ + - ] \d { 4 } / ;
6
8
7
9
let date ;
8
- if ( iso8601Regex . test ( value ) ) {
9
- date = new Date ( value ) ;
10
- } else if ( simpleDateRegex . test ( value ) ) {
11
- date = new Date ( value ) ;
12
- } else {
13
- // Try to parse other formats, such as 'Wed Dec 11 2024 14:46:19 GMT+0200'
14
- try {
10
+
11
+ if ( typeof value === 'number' && value . toString ( ) . length === 10 ) {
12
+ // Handle Unix timestamp (seconds) by converting to milliseconds
13
+ date = new Date ( value * 1000 ) ;
14
+ } else if ( typeof value === 'string' ) {
15
+ if ( iso8601Regex . test ( value ) ) {
16
+ // Handle ISO 8601 format
17
+ date = new Date ( value ) ;
18
+ } else if ( simpleDateRegex . test ( value ) ) {
19
+ // Handle YYYY-MM-DD format
15
20
date = new Date ( value ) ;
16
- if ( isNaN ( date ) ) {
17
- // If invalid, return original value
18
- return value ;
19
- }
20
- } catch ( error ) {
21
- // Return original value if parsing fails
21
+ } else if ( longFormDateRegex . test ( value ) ) {
22
+ // Handle long-form date string
23
+ date = new Date ( value ) ;
24
+ } else {
25
+ // Non-date strings, including IDs, are returned as-is
22
26
return value ;
23
27
}
28
+ } else if ( value instanceof Date ) {
29
+ // Handle Date objects directly
30
+ date = value ;
31
+ } else {
32
+ // For unsupported types, return the original value
33
+ return value ;
24
34
}
35
+
25
36
const options = format === 'datetime'
26
37
? { day : '2-digit' , month : '2-digit' , year : 'numeric' , hour : '2-digit' , minute : '2-digit' , second : '2-digit' }
27
38
: { day : '2-digit' , month : '2-digit' , year : 'numeric' } ;
0 commit comments