From fe48ff340d7d7bdc3321c9292f96b82412ebb3c8 Mon Sep 17 00:00:00 2001 From: Greg Date: Sat, 31 May 2025 08:28:46 +0200 Subject: [PATCH] feat: add core utility functions for date formatting, validation, and UI helpers --- js/utils.js | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/js/utils.js b/js/utils.js index 5dbb02b..a2b93a5 100644 --- a/js/utils.js +++ b/js/utils.js @@ -202,6 +202,41 @@ const Utils = (() => { return dateStr; // Fallback: return original if not recognized }; + /** + * Convert a date string from DD/MM/YYYY to YYYY-MM-DD (ISO). + * @param {string} uiDate - Date in DD/MM/YYYY + * @returns {string} - Date in YYYY-MM-DD + */ + const toISODate = (uiDate) => { + if (!uiDate) return ''; + const parts = uiDate.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/); + if (parts) { + const day = String(parts[1]).padStart(2, '0'); + const month = String(parts[2]).padStart(2, '0'); + const year = parts[3]; + return `${year}-${month}-${day}`; + } + // If already in ISO format, return as is + if (/^\d{4}-\d{2}-\d{2}$/.test(uiDate)) return uiDate; + return uiDate; + }; + + /** + * Convert a date string from YYYY-MM-DD (ISO) to DD/MM/YYYY for UI. + * @param {string} isoDate - Date in YYYY-MM-DD + * @returns {string} - Date in DD/MM/YYYY + */ + const toUIDate = (isoDate) => { + if (!isoDate) return ''; + const parts = isoDate.match(/^(\d{4})-(\d{2})-(\d{2})$/); + if (parts) { + return `${parts[3]}/${parts[2]}/${parts[1]}`; + } + // If already in UI format, return as is + if (/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(isoDate)) return isoDate; + return isoDate; + }; + return { formatDate, getTodayDate, @@ -211,6 +246,7 @@ const Utils = (() => { calculateWeightStats, copyToClipboard, sanitizeHTML, - uiDateToISO // Export the new function + toISODate, + toUIDate }; })();