Files
app/src/admin/utils/formatters.ts
BOHA 1f05ef6a55 fix(dates): use local 'today' for date-input defaults instead of the UTC date
11 form defaults across 6 files used new Date().toISOString().split('T')[0] (the UTC date), which lands on yesterday during the post-midnight Prague window (UTC+1/+2). Added todayLocalStr() (local YYYY-MM-DD) in formatters and replaced them. Future-date expressions (e.g. invoice due_date +14d) left untouched.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-06 02:38:58 +02:00

53 lines
1.7 KiB
TypeScript

export function formatCurrency(
amount: number | string,
currency: string,
): string {
const num = Number(amount) || 0;
switch (currency) {
case "EUR":
return `${num.toLocaleString("cs-CZ", { minimumFractionDigits: 2, maximumFractionDigits: 2 })} €`;
case "USD":
return `$${num.toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
case "CZK":
return `${num.toLocaleString("cs-CZ", { minimumFractionDigits: 2, maximumFractionDigits: 2 })} Kč`;
case "GBP":
return `£${num.toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
default:
return `${num.toFixed(2)} ${currency}`;
}
}
export function formatDate(dateStr: string | null | undefined): string {
if (!dateStr) return "—";
const d = new Date(dateStr);
return d.toLocaleDateString("cs-CZ");
}
/**
* Today's date as `YYYY-MM-DD` in LOCAL (Prague) time — use this for
* date-input defaults. `new Date().toISOString().slice(0, 10)` gives the UTC
* date, which is a day behind the Czech date during the post-midnight window
* (Prague is UTC+1/+2), so a "today" default could land on yesterday.
*/
export function todayLocalStr(): string {
const d = new Date();
const m = String(d.getMonth() + 1).padStart(2, "0");
const day = String(d.getDate()).padStart(2, "0");
return `${d.getFullYear()}-${m}-${day}`;
}
export function formatKm(km: number | string): string {
return new Intl.NumberFormat("cs-CZ").format(Number(km) || 0);
}
export function czechPlural(
n: number,
one: string,
few: string,
many: string,
): string {
if (n === 1) return one;
if (n >= 2 && n <= 4) return few;
return many;
}