Files
courier/lib/format.ts
T
2026-11-23 09:08:21 +00:00

25 lines
976 B
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export function datum(value: string | Date | null | undefined): string {
if (!value) return "–";
return new Intl.DateTimeFormat("de-DE", {
dateStyle: "short",
timeStyle: "medium",
timeZone: "Europe/Berlin",
}).format(new Date(value));
}
export function relativ(value: string | Date | null | undefined): string {
if (!value) return "–";
const diff = (new Date(value).getTime() - Date.now()) / 1000;
const rtf = new Intl.RelativeTimeFormat("de", { numeric: "auto", style: "short" });
const abs = Math.abs(diff);
if (abs < 60) return rtf.format(Math.round(diff), "second");
if (abs < 3600) return rtf.format(Math.round(diff / 60), "minute");
if (abs < 86400) return rtf.format(Math.round(diff / 3600), "hour");
if (abs < 30 * 86400) return rtf.format(Math.round(diff / 86400), "day");
return datum(value);
}
export function kurz(value: string, max = 42): string {
return value.length > max ? `${value.slice(0, max - 1)}…` : value;
}