26 lines
925 B
TypeScript
26 lines
925 B
TypeScript
export function formatPrice(price: string | null | undefined, currency: string): string | null {
|
|
if (!price) return null;
|
|
const n = Number(price);
|
|
if (Number.isNaN(n)) return `${price} ${currency}`;
|
|
try {
|
|
return new Intl.NumberFormat(undefined, {
|
|
style: 'currency',
|
|
currency,
|
|
maximumFractionDigits: 2,
|
|
}).format(n);
|
|
} catch {
|
|
return `${n.toLocaleString()} ${currency}`;
|
|
}
|
|
}
|
|
|
|
export function formatDate(iso: string | Date): string {
|
|
const d = typeof iso === 'string' ? new Date(iso) : iso;
|
|
return d.toLocaleDateString(undefined, { day: 'numeric', month: 'short', year: 'numeric' });
|
|
}
|
|
|
|
export function daysLeftUntil(iso: string | Date, retentionDays: number): number {
|
|
const d = typeof iso === 'string' ? new Date(iso) : iso;
|
|
const expires = d.getTime() + retentionDays * 24 * 60 * 60 * 1000;
|
|
return Math.max(0, Math.ceil((expires - Date.now()) / (24 * 60 * 60 * 1000)));
|
|
}
|