Files
CapaKraken/apps/web/src/lib/format.ts
T
Hartmut 1df208dbcc feat(timeline): add pulse animation for in-flight drag mutations
Allocation bars that have active optimistic overrides (post-drag,
awaiting server confirmation) now pulse subtly via animate-pulse.
The pending set is derived from the existing optimisticAllocations
map keys, requiring no additional state.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-09 13:28:46 +02:00

74 lines
2.5 KiB
TypeScript

/**
* Format a Date for an HTML <input type="date"> value (yyyy-mm-dd, local timezone).
* Returns "" for null/undefined.
*/
export function toDateInputValue(date: Date | string | null | undefined): string {
if (!date) return "";
const d = typeof date === "string" ? new Date(date) : date;
const y = d.getFullYear();
const m = String(d.getMonth() + 1).padStart(2, "0");
const day = String(d.getDate()).padStart(2, "0");
return `${y}-${m}-${day}`;
}
/**
* Format a date as dd/mm/yyyy for display in the UI.
* Input date inputs (type="date") still use yyyy-mm-dd — this is for rendered text only.
*/
export function formatDate(d: Date | string | null | undefined): string {
if (!d) return "";
return new Date(d).toLocaleDateString("en-GB"); // en-GB → dd/mm/yyyy
}
/**
* Format a date as "DD MMM" (e.g. "04 Mar") for compact timeline labels.
*/
export function formatDateShort(d: Date | string): string {
return new Date(d).toLocaleDateString("en-GB", { day: "2-digit", month: "short" });
}
/**
* Format a date as "MMM YY" (e.g. "Mar 26") for timeline month headers.
*/
export function formatMonthYear(d: Date | string): string {
return new Date(d).toLocaleDateString("en-GB", { month: "short", year: "2-digit" });
}
/**
* Format a date in long form (e.g. "4 March 2026") for descriptive contexts.
*/
export function formatDateLong(d: Date | string): string {
return new Date(d).toLocaleDateString("en-GB", { day: "numeric", month: "long", year: "numeric" });
}
/**
* Format integer cents as a currency string (e.g. "1.234 €").
* Defaults to EUR with no decimal places. Pass `fractionDigits: 2` for precise display.
*/
export function formatMoney(cents: number | null | undefined, currency = "EUR", fractionDigits = 0): string {
const value = (cents ?? 0) / 100;
return new Intl.NumberFormat("de-DE", {
style: "currency",
currency,
minimumFractionDigits: fractionDigits,
maximumFractionDigits: fractionDigits,
}).format(value);
}
/**
* Format a date as "16 Mar 2026" for compact display with year.
*/
export function formatDateMedium(d: Date | string | null | undefined): string {
if (!d) return "";
return new Date(d).toLocaleDateString("en-GB", { year: "numeric", month: "short", day: "numeric" });
}
/**
* Format integer cents as a plain decimal string (e.g. "12,34").
* Returns "-" for null/undefined.
*/
export function formatCents(cents: number | null | undefined): string {
if (cents == null) return "-";
return (cents / 100).toLocaleString("de-DE", { minimumFractionDigits: 2, maximumFractionDigits: 2 });
}