1
0
Fork 0
mirror of synced 2024-07-08 15:56:23 +12:00

Update date formatting to use default locale as specified by Intl browser API rather than navigator language

This commit is contained in:
Andrew Kingston 2024-04-26 12:40:15 +01:00
parent 7c263d940a
commit 67863da655

View file

@ -186,8 +186,7 @@ export const stringifyDate = (
} }
} }
// Gets the correct format for a date in a specific locale // Determine the dayjs-compatible format of the browser's default locale
const getDateFormatPattern = locale => {
const getPatternForPart = part => { const getPatternForPart = part => {
switch (part.type) { switch (part.type) {
case "day": case "day":
@ -203,13 +202,10 @@ const getDateFormatPattern = locale => {
return "" return ""
} }
} }
return new Intl.DateTimeFormat(locale) const localeDateFormat = new Intl.DateTimeFormat()
.formatToParts(new Date("2021-01-01")) .formatToParts(new Date("2021-01-01"))
.map(getPatternForPart) .map(getPatternForPart)
.join("") .join("")
}
const localeDateFormat = getDateFormatPattern(navigator.language || "en-US")
// Formats a dayjs date according to schema flags // Formats a dayjs date according to schema flags
export const getDateDisplayValue = ( export const getDateDisplayValue = (
@ -224,6 +220,6 @@ export const getDateDisplayValue = (
} else if (!enableTime) { } else if (!enableTime) {
return value.format(localeDateFormat) return value.format(localeDateFormat)
} else { } else {
return value.format(`${localeDateFormat}, HH:mm`) return value.format(`${localeDateFormat} HH:mm`)
} }
} }