From e1aca6b00c5affb914a0db44a6264c8e54fdffd6 Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 24 Jul 2025 11:58:45 +0200 Subject: [PATCH] fix: auto-convert old date formats to new date-fns syntax MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add convertDateFormat function to automatically fix DD->dd, YYYY->yyyy - Handles existing database values with old format strings - Prevents RangeError when using old formats stored in settings - Ensures backward compatibility without requiring database updates This fix converts formats on-the-fly so existing production data with old formats like 'DD.MM.YYYY' will work correctly. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- frontend/src/hooks/useLocalizedDate.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/frontend/src/hooks/useLocalizedDate.ts b/frontend/src/hooks/useLocalizedDate.ts index f2ad5ad..f086a50 100644 --- a/frontend/src/hooks/useLocalizedDate.ts +++ b/frontend/src/hooks/useLocalizedDate.ts @@ -4,6 +4,14 @@ import { de, enUS } from 'date-fns/locale'; import { useQuery } from '@tanstack/react-query'; import { publicSettingsService } from '../services/publicSettings.service'; +// Convert old date format strings to new date-fns format +const convertDateFormat = (format: string): string => { + return format + .replace(/DD/g, 'dd') // Days: DD -> dd + .replace(/YYYY/g, 'yyyy') // Years: YYYY -> yyyy + .replace(/YY/g, 'yy'); // Short years: YY -> yy +}; + export const useLocalizedDate = () => { const { i18n } = useTranslation(); @@ -30,6 +38,10 @@ export const useLocalizedDate = () => { : settings.general_date_format.format || 'PPP'; } dateFormat = dateFormat || 'PPP'; + + // Convert old format to new format + dateFormat = convertDateFormat(dateFormat); + return dateFnsFormat(dateObj, dateFormat, { locale: getLocale() }); }; @@ -43,9 +55,11 @@ export const useLocalizedDate = () => { formatDistanceToNow, locale: getLocale(), dateFormat: settings?.general_date_format - ? (typeof settings.general_date_format === 'string' - ? settings.general_date_format - : settings.general_date_format.format || 'PPP') + ? convertDateFormat( + typeof settings.general_date_format === 'string' + ? settings.general_date_format + : settings.general_date_format.format || 'PPP' + ) : 'PPP' }; }; \ No newline at end of file