From 34846ae71aea7f60ea45ee133dbdba2c7795a3bc Mon Sep 17 00:00:00 2001 From: paul Date: Wed, 16 Jul 2025 22:41:40 +0200 Subject: [PATCH] fix: resolve Invalid Date issue in email templates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fix date parsing for YYYY-MM-DD format to use local timezone - Handle date strings properly to avoid timezone conversion issues - Add validation to ensure dates are valid before formatting - Update expiration date calculation to use consistent parsing This fixes the "Invalid Date" display in gallery creation emails. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- backend/src/routes/adminEvents.js | 9 +++- backend/src/utils/dateFormatter.js | 77 +++++++++++++++++++----------- 2 files changed, 57 insertions(+), 29 deletions(-) diff --git a/backend/src/routes/adminEvents.js b/backend/src/routes/adminEvents.js index d50a47a..d209192 100644 --- a/backend/src/routes/adminEvents.js +++ b/backend/src/routes/adminEvents.js @@ -89,7 +89,14 @@ router.post('/', adminAuth, [ const password_hash = await bcrypt.hash(password, getBcryptRounds()); // Calculate expiration date (days after event date) - const expires_at = new Date(event_date); + // Parse YYYY-MM-DD format as local date to avoid timezone issues + let expires_at; + if (event_date.match(/^\d{4}-\d{2}-\d{2}$/)) { + const [year, month, day] = event_date.split('-').map(num => parseInt(num, 10)); + expires_at = new Date(year, month - 1, day); + } else { + expires_at = new Date(event_date); + } expires_at.setDate(expires_at.getDate() + parseInt(expiration_days, 10)); // Create folder structure diff --git a/backend/src/utils/dateFormatter.js b/backend/src/utils/dateFormatter.js index 02f1485..bc7ff07 100644 --- a/backend/src/utils/dateFormatter.js +++ b/backend/src/utils/dateFormatter.js @@ -27,7 +27,28 @@ async function formatDate(date, language = 'en') { } } - const dateObj = date instanceof Date ? date : new Date(date); + // Ensure proper date parsing + let dateObj; + if (date instanceof Date) { + dateObj = date; + } else if (typeof date === 'string') { + // For date strings like "2025-07-16", parse as local date to avoid timezone issues + if (date.match(/^\d{4}-\d{2}-\d{2}$/)) { + // Parse YYYY-MM-DD format as local date + const [year, month, day] = date.split('-').map(num => parseInt(num, 10)); + dateObj = new Date(year, month - 1, day); + } else { + dateObj = new Date(date); + } + } else { + dateObj = new Date(date); + } + + // Check if date is valid + if (isNaN(dateObj.getTime())) { + console.error('Invalid date provided to formatDate:', date); + throw new Error('Invalid date'); + } // Use appropriate locale based on language let locale = dateConfig.locale || 'en-GB'; @@ -39,33 +60,33 @@ async function formatDate(date, language = 'en') { // Format based on the configured format switch (dateConfig.format) { - case 'MM/DD/YYYY': - return dateObj.toLocaleDateString(locale, { - month: '2-digit', - day: '2-digit', - year: 'numeric' - }); - case 'DD/MM/YYYY': - return dateObj.toLocaleDateString(locale, { - day: '2-digit', - month: '2-digit', - year: 'numeric' - }); - case 'YYYY-MM-DD': - return dateObj.toISOString().split('T')[0]; - case 'DD.MM.YYYY': - return dateObj.toLocaleDateString('de-DE', { - day: '2-digit', - month: '2-digit', - year: 'numeric' - }); - default: - // Use long format as fallback - return dateObj.toLocaleDateString(locale, { - year: 'numeric', - month: 'long', - day: 'numeric' - }); + case 'MM/DD/YYYY': + return dateObj.toLocaleDateString(locale, { + month: '2-digit', + day: '2-digit', + year: 'numeric' + }); + case 'DD/MM/YYYY': + return dateObj.toLocaleDateString(locale, { + day: '2-digit', + month: '2-digit', + year: 'numeric' + }); + case 'YYYY-MM-DD': + return dateObj.toISOString().split('T')[0]; + case 'DD.MM.YYYY': + return dateObj.toLocaleDateString('de-DE', { + day: '2-digit', + month: '2-digit', + year: 'numeric' + }); + default: + // Use long format as fallback + return dateObj.toLocaleDateString(locale, { + year: 'numeric', + month: 'long', + day: 'numeric' + }); } } catch (error) { console.error('Error formatting date:', error);