50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
// Datums- und Formatierungshelfer für den Wochenplan (Mo–Fr).
|
||
// Alle Berechnungen laufen über UTC, damit Server und Browser
|
||
// unabhängig von der Zeitzone dieselbe Woche ermitteln.
|
||
|
||
const ISO_DATE = /^\d{4}-\d{2}-\d{2}$/;
|
||
|
||
function toDate(iso: string): Date {
|
||
return new Date(`${iso}T00:00:00Z`);
|
||
}
|
||
|
||
export function toISO(d: Date): string {
|
||
return d.toISOString().slice(0, 10);
|
||
}
|
||
|
||
/** Montag der Woche, in der `iso` liegt (bzw. der aktuellen Woche). */
|
||
export function mondayOf(iso?: string | null): string {
|
||
const base =
|
||
iso && ISO_DATE.test(iso)
|
||
? toDate(iso)
|
||
: new Date();
|
||
const day = (base.getUTCDay() + 6) % 7; // Montag = 0
|
||
const monday = new Date(
|
||
Date.UTC(base.getUTCFullYear(), base.getUTCMonth(), base.getUTCDate() - day)
|
||
);
|
||
return toISO(monday);
|
||
}
|
||
|
||
export function addDays(iso: string, days: number): string {
|
||
const d = toDate(iso);
|
||
d.setUTCDate(d.getUTCDate() + days);
|
||
return toISO(d);
|
||
}
|
||
|
||
export const WEEKDAYS = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag"];
|
||
|
||
/** z. B. "13. Juli 2026" */
|
||
export function formatDateLong(iso: string): string {
|
||
return toDate(iso).toLocaleDateString("de-DE", {
|
||
day: "numeric",
|
||
month: "long",
|
||
year: "numeric",
|
||
timeZone: "UTC",
|
||
});
|
||
}
|
||
|
||
/** z. B. "3,50€" */
|
||
export function formatPrice(price: number | string): string {
|
||
return Number(price).toFixed(2).replace(".", ",") + "€";
|
||
}
|