mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-08-27 21:55:15 -05:00
Fix Swedish AM/PM markers leaking into time when YMD date format set
This commit is contained in:
@@ -84,9 +84,23 @@ export function useTimeFormat() {
|
||||
|
||||
const formatDateTime = (date: Date, options?: Intl.DateTimeFormatOptions) => {
|
||||
const adjusted = withYearFirstAdjustment(options, dateFormat);
|
||||
const useDateLocale = isNumericMonth(adjusted);
|
||||
const hasTimePart = Boolean(adjusted?.hour);
|
||||
|
||||
// When the user's date-format preference forces a non-language locale (e.g. sv-SE
|
||||
// for YMD), applying it to the full date+time would also pull in that locale's
|
||||
// time conventions — most visibly Swedish "fm"/"em" instead of "AM"/"PM".
|
||||
// Format the date and time portions separately to keep them locale-correct.
|
||||
if (hasTimePart && useDateLocale && dateLocale !== i18n.language) {
|
||||
const { hour, minute, second, timeZoneName, ...dateOptions } = adjusted!;
|
||||
const datePart = date.toLocaleDateString(dateLocale, dateOptions);
|
||||
const timePart = formatTime(date, { hour, minute, second, timeZoneName });
|
||||
return `${datePart}, ${timePart}`;
|
||||
}
|
||||
|
||||
const result = date.toLocaleString(
|
||||
isNumericMonth(adjusted) ? dateLocale : i18n.language,
|
||||
adjusted?.hour
|
||||
useDateLocale ? dateLocale : i18n.language,
|
||||
hasTimePart
|
||||
? {
|
||||
...adjusted,
|
||||
...clockOptions,
|
||||
@@ -95,7 +109,7 @@ export function useTimeFormat() {
|
||||
...adjusted,
|
||||
},
|
||||
);
|
||||
return clockOptions.hourCycle === "h23" && adjusted?.hour
|
||||
return clockOptions.hourCycle === "h23" && hasTimePart
|
||||
? stripLeadingZeroFromHour(result)
|
||||
: result;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user