From eb69ea74c16ed442025f84746ebb80bd59b35ac3 Mon Sep 17 00:00:00 2001 From: gkatrakazas Date: Fri, 29 Nov 2024 15:04:50 +0200 Subject: [PATCH] Handle all date format cases --- src/functions/DateFormat.js | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/functions/DateFormat.js b/src/functions/DateFormat.js index c76335bc..b34840bf 100644 --- a/src/functions/DateFormat.js +++ b/src/functions/DateFormat.js @@ -3,25 +3,36 @@ export function formatDate(value, format = 'datetime') { const iso8601Regex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/; // Regex for simple YYYY-MM-DD format const simpleDateRegex = /^\d{4}-\d{2}-\d{2}$/; + // Regex for long-form date strings like 'Wed Dec 11 2024 14:46:19 GMT+0200' + const longFormDateRegex = /^[A-Z][a-z]{2} [A-Z][a-z]{2} \d{2} \d{4} \d{2}:\d{2}:\d{2} GMT[+-]\d{4}/; let date; - if (iso8601Regex.test(value)) { - date = new Date(value); - } else if (simpleDateRegex.test(value)) { - date = new Date(value); - } else { - // Try to parse other formats, such as 'Wed Dec 11 2024 14:46:19 GMT+0200' - try { + + if (typeof value === 'number' && value.toString().length === 10) { + // Handle Unix timestamp (seconds) by converting to milliseconds + date = new Date(value * 1000); + } else if (typeof value === 'string') { + if (iso8601Regex.test(value)) { + // Handle ISO 8601 format + date = new Date(value); + } else if (simpleDateRegex.test(value)) { + // Handle YYYY-MM-DD format date = new Date(value); - if (isNaN(date)) { - // If invalid, return original value - return value; - } - } catch (error) { - // Return original value if parsing fails + } else if (longFormDateRegex.test(value)) { + // Handle long-form date string + date = new Date(value); + } else { + // Non-date strings, including IDs, are returned as-is return value; } + } else if (value instanceof Date) { + // Handle Date objects directly + date = value; + } else { + // For unsupported types, return the original value + return value; } + const options = format === 'datetime' ? { day: '2-digit', month: '2-digit', year: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit' } : { day: '2-digit', month: '2-digit', year: 'numeric' };