38 lines
950 B
TypeScript
38 lines
950 B
TypeScript
const DASH = "—";
|
|
|
|
const DATE_TIME_FORMATTER = new Intl.DateTimeFormat("en-GB", {
|
|
timeZone: "UTC",
|
|
year: "numeric",
|
|
month: "2-digit",
|
|
day: "2-digit",
|
|
hour: "2-digit",
|
|
minute: "2-digit",
|
|
second: "2-digit",
|
|
hour12: false,
|
|
});
|
|
|
|
const DATE_FORMATTER = new Intl.DateTimeFormat("en-GB", {
|
|
timeZone: "UTC",
|
|
year: "numeric",
|
|
month: "2-digit",
|
|
day: "2-digit",
|
|
});
|
|
|
|
function toValidDate(value?: string | Date | null): Date | null {
|
|
if (!value) return null;
|
|
const date = value instanceof Date ? value : new Date(value);
|
|
return Number.isNaN(date.getTime()) ? null : date;
|
|
}
|
|
|
|
export function formatDateTime(value?: string | Date | null): string {
|
|
const date = toValidDate(value);
|
|
if (!date) return DASH;
|
|
return `${DATE_TIME_FORMATTER.format(date)} UTC`;
|
|
}
|
|
|
|
export function formatDate(value?: string | Date | null): string {
|
|
const date = toValidDate(value);
|
|
if (!date) return DASH;
|
|
return DATE_FORMATTER.format(date);
|
|
}
|