Always use year: numeric if YMD date format preference

This commit is contained in:
Kalle
2026-04-28 21:15:03 +03:00
parent 06c2f637a3
commit a1491a189e

View File

@@ -83,18 +83,19 @@ export function useTimeFormat() {
const dateLocale = getDateLocale(dateFormat, i18n.language);
const formatDateTime = (date: Date, options?: Intl.DateTimeFormatOptions) => {
const adjusted = withYearFirstAdjustment(options, dateFormat);
const result = date.toLocaleString(
isNumericMonth(options) ? dateLocale : i18n.language,
options?.hour
isNumericMonth(adjusted) ? dateLocale : i18n.language,
adjusted?.hour
? {
...options,
...adjusted,
...clockOptions,
}
: {
...options,
...adjusted,
},
);
return clockOptions.hourCycle === "h23" && options?.hour
return clockOptions.hourCycle === "h23" && adjusted?.hour
? stripLeadingZeroFromHour(result)
: result;
};
@@ -116,9 +117,10 @@ export function useTimeFormat() {
};
const formatDate = (date: Date, options?: Intl.DateTimeFormatOptions) => {
const adjusted = withYearFirstAdjustment(options, dateFormat);
return date.toLocaleDateString(
isNumericMonth(options) ? dateLocale : i18n.language,
options,
isNumericMonth(adjusted) ? dateLocale : i18n.language,
adjusted,
);
};
@@ -193,3 +195,12 @@ function isNumericMonth(options: Intl.DateTimeFormatOptions | undefined) {
if (!options?.month) return false;
return options.month === "numeric" || options.month === "2-digit";
}
function withYearFirstAdjustment(
options: Intl.DateTimeFormatOptions | undefined,
dateFormat: UserPreferences["dateFormat"] | undefined,
): Intl.DateTimeFormatOptions | undefined {
if (options?.year !== "2-digit") return options;
if (dateFormat !== "YMD") return options;
return { ...options, year: "numeric" };
}