feat: add core utility functions for date formatting, validation, and UI helpers
This commit is contained in:
parent
553de56a83
commit
fe48ff340d
38
js/utils.js
38
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
|
||||
};
|
||||
})();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user