fix: auto-convert old date formats to new date-fns syntax
- 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 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,14 @@ import { de, enUS } from 'date-fns/locale';
|
|||||||
import { useQuery } from '@tanstack/react-query';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import { publicSettingsService } from '../services/publicSettings.service';
|
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 = () => {
|
export const useLocalizedDate = () => {
|
||||||
const { i18n } = useTranslation();
|
const { i18n } = useTranslation();
|
||||||
|
|
||||||
@@ -30,6 +38,10 @@ export const useLocalizedDate = () => {
|
|||||||
: settings.general_date_format.format || 'PPP';
|
: settings.general_date_format.format || 'PPP';
|
||||||
}
|
}
|
||||||
dateFormat = dateFormat || 'PPP';
|
dateFormat = dateFormat || 'PPP';
|
||||||
|
|
||||||
|
// Convert old format to new format
|
||||||
|
dateFormat = convertDateFormat(dateFormat);
|
||||||
|
|
||||||
return dateFnsFormat(dateObj, dateFormat, { locale: getLocale() });
|
return dateFnsFormat(dateObj, dateFormat, { locale: getLocale() });
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -43,9 +55,11 @@ export const useLocalizedDate = () => {
|
|||||||
formatDistanceToNow,
|
formatDistanceToNow,
|
||||||
locale: getLocale(),
|
locale: getLocale(),
|
||||||
dateFormat: settings?.general_date_format
|
dateFormat: settings?.general_date_format
|
||||||
? (typeof settings.general_date_format === 'string'
|
? convertDateFormat(
|
||||||
? settings.general_date_format
|
typeof settings.general_date_format === 'string'
|
||||||
: settings.general_date_format.format || 'PPP')
|
? settings.general_date_format
|
||||||
|
: settings.general_date_format.format || 'PPP'
|
||||||
|
)
|
||||||
: 'PPP'
|
: 'PPP'
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user