mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-09-08 20:26:08 -05:00
Always use year: numeric if YMD date format preference
This commit is contained in:
@@ -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" };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user