2020-10-01 15:07:15 +02:00
|
|
|
import { DateTimeFormatOptions } from "vue-i18n";
|
2021-03-08 18:37:25 +01:00
|
|
|
import { i18n } from "../utils/i18n";
|
2020-10-01 15:07:15 +02:00
|
|
|
|
2019-09-09 09:31:08 +02:00
|
|
|
function parseDateTime(value: string): Date {
|
|
|
|
return new Date(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatDateString(value: string): string {
|
2021-03-08 18:37:25 +01:00
|
|
|
return parseDateTime(value).toLocaleString(locale(), {
|
2020-02-18 08:57:00 +01:00
|
|
|
weekday: "long",
|
|
|
|
year: "numeric",
|
|
|
|
month: "long",
|
|
|
|
day: "numeric",
|
|
|
|
});
|
2019-09-09 09:31:08 +02:00
|
|
|
}
|
|
|
|
|
2021-10-10 16:25:50 +02:00
|
|
|
function formatTimeString(value: string, timeZone: string): string {
|
2021-03-08 18:37:25 +01:00
|
|
|
return parseDateTime(value).toLocaleTimeString(locale(), {
|
2020-11-30 10:24:11 +01:00
|
|
|
hour: "numeric",
|
|
|
|
minute: "numeric",
|
2021-10-10 16:25:50 +02:00
|
|
|
timeZone,
|
2020-11-30 10:24:11 +01:00
|
|
|
});
|
2019-09-09 09:31:08 +02:00
|
|
|
}
|
|
|
|
|
2021-07-23 15:47:10 +02:00
|
|
|
// TODO: These can be removed in favor of dateStyle/timeStyle when those two have sufficient support
|
|
|
|
// https://caniuse.com/mdn-javascript_builtins_intl_datetimeformat_datetimeformat_datestyle
|
|
|
|
const LONG_DATE_FORMAT_OPTIONS: DateTimeFormatOptions = {
|
|
|
|
weekday: undefined,
|
|
|
|
year: "numeric",
|
|
|
|
month: "long",
|
|
|
|
day: "numeric",
|
|
|
|
hour: 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,
|
2021-11-22 20:00:01 +01:00
|
|
|
timeZone: string | null | undefined = undefined,
|
2021-07-23 15:47:10 +02:00
|
|
|
showTime = true,
|
|
|
|
dateFormat = "long"
|
|
|
|
): string {
|
|
|
|
const isLongFormat = dateFormat === "long";
|
|
|
|
let options = isLongFormat
|
|
|
|
? LONG_DATE_FORMAT_OPTIONS
|
|
|
|
: SHORT_DATE_FORMAT_OPTIONS;
|
2019-10-14 19:29:18 +02:00
|
|
|
if (showTime) {
|
2021-07-23 15:47:10 +02:00
|
|
|
options = {
|
|
|
|
...options,
|
|
|
|
...(isLongFormat ? LONG_TIME_FORMAT_OPTIONS : SHORT_TIME_FORMAT_OPTIONS),
|
2021-11-22 20:00:01 +01:00
|
|
|
timeZone: timeZone || undefined,
|
2021-07-23 15:47:10 +02:00
|
|
|
};
|
2019-10-14 19:29:18 +02:00
|
|
|
}
|
2021-03-08 18:37:25 +01:00
|
|
|
const format = new Intl.DateTimeFormat(locale(), options);
|
2020-10-01 15:07:15 +02:00
|
|
|
return format.format(parseDateTime(value));
|
2019-09-09 09:31:08 +02:00
|
|
|
}
|
|
|
|
|
2021-03-08 18:37:25 +01:00
|
|
|
const locale = () => i18n.locale.replace("_", "-");
|
|
|
|
|
2019-09-09 09:31:08 +02:00
|
|
|
export { formatDateString, formatTimeString, formatDateTimeString };
|