fix: auto-convert old date formats to new date-fns syntax
Mirror to GitHub / mirror (push) Successful in 51s
Test and Lint / backend-test (push) Successful in 1m42s
continuous-integration/drone/push Build is passing
Test and Lint / frontend-test (push) Successful in 2m8s
Version and Release / version-bump (push) Successful in 55s
Version and Release / trigger-drone (push) Successful in 3s
Mirror to GitHub / mirror (push) Successful in 51s
Test and Lint / backend-test (push) Successful in 1m42s
continuous-integration/drone/push Build is passing
Test and Lint / frontend-test (push) Successful in 2m8s
Version and Release / version-bump (push) Successful in 55s
Version and Release / trigger-drone (push) Successful in 3s
- 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 { 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'
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user