Add short format for datetimes
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
c394f2cc5a
commit
8d23fca9a0
@ -21,19 +21,52 @@ function formatTimeString(value: string): string {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatDateTimeString(value: string, showTime = true): string {
|
// TODO: These can be removed in favor of dateStyle/timeStyle when those two have sufficient support
|
||||||
const options: DateTimeFormatOptions = {
|
// https://caniuse.com/mdn-javascript_builtins_intl_datetimeformat_datetimeformat_datestyle
|
||||||
|
const LONG_DATE_FORMAT_OPTIONS: DateTimeFormatOptions = {
|
||||||
weekday: undefined,
|
weekday: undefined,
|
||||||
year: "numeric",
|
year: "numeric",
|
||||||
month: "long",
|
month: "long",
|
||||||
day: "numeric",
|
day: "numeric",
|
||||||
hour: undefined,
|
hour: undefined,
|
||||||
minute: undefined,
|
minute: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const LONG_TIME_FORMAT_OPTIONS: DateTimeFormatOptions = {
|
||||||
|
weekday: "long",
|
||||||
|
hour: "numeric",
|
||||||
|
minute: "numeric",
|
||||||
|
};
|
||||||
|
|
||||||
|
const SHORT_DATE_FORMAT_OPTIONS: DateTimeFormatOptions = {
|
||||||
|
weekday: undefined,
|
||||||
|
year: "numeric",
|
||||||
|
month: "short",
|
||||||
|
day: "numeric",
|
||||||
|
hour: undefined,
|
||||||
|
minute: undefined,
|
||||||
|
};
|
||||||
|
|
||||||
|
const SHORT_TIME_FORMAT_OPTIONS: DateTimeFormatOptions = {
|
||||||
|
weekday: "short",
|
||||||
|
hour: "numeric",
|
||||||
|
minute: "numeric",
|
||||||
|
};
|
||||||
|
|
||||||
|
function formatDateTimeString(
|
||||||
|
value: string,
|
||||||
|
showTime = true,
|
||||||
|
dateFormat = "long"
|
||||||
|
): string {
|
||||||
|
const isLongFormat = dateFormat === "long";
|
||||||
|
let options = isLongFormat
|
||||||
|
? LONG_DATE_FORMAT_OPTIONS
|
||||||
|
: SHORT_DATE_FORMAT_OPTIONS;
|
||||||
if (showTime) {
|
if (showTime) {
|
||||||
options.weekday = "long";
|
options = {
|
||||||
options.hour = "numeric";
|
...options,
|
||||||
options.minute = "numeric";
|
...(isLongFormat ? LONG_TIME_FORMAT_OPTIONS : SHORT_TIME_FORMAT_OPTIONS),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
const format = new Intl.DateTimeFormat(locale(), options);
|
const format = new Intl.DateTimeFormat(locale(), options);
|
||||||
return format.format(parseDateTime(value));
|
return format.format(parseDateTime(value));
|
||||||
|
Loading…
Reference in New Issue
Block a user