Booking page now displays localized dates, times, and timezones (#7130)
This commit is contained in:
1 parent
78656f2116
commit
09cc6f07e4
2 files changed
+80
-14
No files matched your search
@@ -29,6 +29,61 @@ export const formatTime = (
|
||||
: dayjs(date).format(timeFormat === 12 ? "h:mma" : "HH:mm");
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a localized and translated date or time, based on the native
|
||||
* Intl.DateTimeFormat available to JS. Undefined values mean the browser's
|
||||
* locale will be used.
|
||||
*/
|
||||
export const formatLocalizedDateTime = (
|
||||
date: Date | Dayjs,
|
||||
options: Intl.DateTimeFormatOptions = {},
|
||||
locale: string | undefined = undefined
|
||||
) => {
|
||||
const theDate = date instanceof dayjs ? (date as Dayjs).toDate() : (date as Date);
|
||||
return Intl.DateTimeFormat(locale, options).format(theDate);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a localized and translated calendar day based on the
|
||||
* given Date object and locale. Undefined values mean the defaults
|
||||
* associated with the browser's current locale will be used.
|
||||
*/
|
||||
export const formatToLocalizedDate = (
|
||||
date: Date | Dayjs,
|
||||
locale: string | undefined = undefined,
|
||||
dateStyle: Intl.DateTimeFormatOptions["dateStyle"] = "long"
|
||||
) => formatLocalizedDateTime(date, { dateStyle }, locale);
|
||||
|
||||
/**
|
||||
* Returns a localized and translated time of day based on the
|
||||
* given Date object and locale. Undefined values mean the defaults
|
||||
* associated with the browser's current locale will be used.
|
||||
*/
|
||||
export const formatToLocalizedTime = (
|
||||
date: Date | Dayjs,
|
||||
locale: string | undefined = undefined,
|
||||
timeStyle: Intl.DateTimeFormatOptions["timeStyle"] = "short",
|
||||
hour12: Intl.DateTimeFormatOptions["hour12"] = undefined
|
||||
) => formatLocalizedDateTime(date, { timeStyle, hour12 }, locale);
|
||||
|
||||
/**
|
||||
* Returns a translated timezone based on the given Date object and
|
||||
* locale. Undefined values mean the browser's current locale
|
||||
* will be used.
|
||||
*/
|
||||
export const formatToLocalizedTimezone = (
|
||||
date: Date | Dayjs,
|
||||
locale: string | undefined = undefined,
|
||||
timeZoneName: Intl.DateTimeFormatOptions["timeZoneName"] = "long"
|
||||
) => {
|
||||
// Intl.DateTimeFormat doesn't format into a timezone only, so we must
|
||||
// formatToParts() and return the piece we want
|
||||
const theDate = date instanceof dayjs ? (date as Dayjs).toDate() : (date as Date);
|
||||
return Intl.DateTimeFormat(locale, { timeZoneName })
|
||||
.formatToParts(theDate)
|
||||
.find((d) => d.type == "timeZoneName")?.value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sorts two timezones by their offset from GMT.
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user