Files
petfeed/mobile/src/lib/date-format.ts
T
adminandClaude Opus 5.5 c74bbffccb feat: Datumseingabe TT.MM.JJJJ (App) + Erinnerung an Folgeimpfung
Datum (found.md, iOS-Feedback): neue DateField-Komponente mit Ziffern-
tastatur und automatisch eingefügten Punkten (TT.MM.JJJJ) für Geburtstag,
Adoptionstag, Gewicht, Tierarzt und Impfungen. Parser akzeptiert weiter
"-" und "/" sowie einstellige Tage/Monate. Wheel-Picker bleibt in
PENDING-NATIVE-BUILD.md (natives Modul).

Impf-Erinnerung (found.md, iOS-Feedback): täglicher Cron
/api/cron/vaccine-reminders erinnert einmalig 30 Tage vor
Vaccine.nextDueDate per Mitteilung (VACCINE_DUE) + Push mit Impfname und
Datum. Schema: NotificationType.VACCINE_DUE, Notification.vaccineId
(Cascade), Vaccine.reminderSentAt (Claim gegen Doppelversand, wird bei
Fehler wieder freigegeben). Web + App zeigen die Mitteilung an, Tippen
öffnet die Gesundheitsseite. App: Schnellauswahl +1/+3 Jahre für den
Folgetermin plus Hinweis auf die Erinnerung.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 12:42:20 +02:00

72 lines
3.0 KiB
TypeScript

/**
* The health forms take a free-text date field validated against the
* backend's strict `YYYY-MM-DD` regex (src/trpc/routers/health.ts). Typing
* dates in that order isn't the German convention — found.md: users typing
* the natural order got a validation error the mutations never surfaced (no
* onError handler), which looked exactly like "data isn't being saved". Fix:
* accept DD.MM.YYYY in the UI, convert to ISO right before the mutation call,
* and reject anything that doesn't parse as a real calendar date instead of
* silently sending garbage.
*
* Display uses "." (found.md Neu7: "-" is unusual in German). Parsing still
* accepts "-" and "/" and one-digit day/month so older habits keep working.
*/
const DISPLAY_SEPARATOR = ".";
const DISPLAY_PATTERN = /^(\d{1,2})[.\-/](\d{1,2})[.\-/](\d{4})$/;
const ISO_PATTERN = /^(\d{4})-(\d{2})-(\d{2})$/;
const MAX_DATE_DIGITS = 8;
export function isoToDisplayDate(iso: string): string {
const match = ISO_PATTERN.exec(iso);
if (!match) return iso;
const [, year, month, day] = match;
return [day, month, year].join(DISPLAY_SEPARATOR);
}
/** Returns the equivalent `YYYY-MM-DD` string, or null if not a valid DD.MM.YYYY calendar date. */
export function displayDateToIso(display: string): string | null {
const match = DISPLAY_PATTERN.exec(display.trim());
if (!match) return null;
const [, dayStr, monthStr, yearStr] = match;
const day = Number(dayStr);
const month = Number(monthStr);
const year = Number(yearStr);
const date = new Date(Date.UTC(year, month - 1, day));
const isRealDate =
date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day;
if (!isRealDate) return null;
return `${yearStr}-${monthStr.padStart(2, "0")}-${dayStr.padStart(2, "0")}`;
}
/**
* Live mask for typing: keeps digits only and inserts the separators itself
* (so the number pad, which has no "." on iOS, is enough). A separator is only
* added once a following digit exists — otherwise backspace over "04." would
* immediately re-add the dot and the field would get stuck.
*/
export function formatDateInput(text: string): string {
const digits = text.replace(/\D/g, "").slice(0, MAX_DATE_DIGITS);
if (digits.length <= 2) return digits;
if (digits.length <= 4) return `${digits.slice(0, 2)}${DISPLAY_SEPARATOR}${digits.slice(2)}`;
return `${digits.slice(0, 2)}${DISPLAY_SEPARATOR}${digits.slice(2, 4)}${DISPLAY_SEPARATOR}${digits.slice(4)}`;
}
/**
* Folgetermin-Vorschlag „+1 / +3 Jahre“ für Impfungen. Null bei ungültigem
* Ausgangsdatum. 29.02. + 1 Jahr landet (wie Date selbst) auf dem 01.03.
*/
export function addYearsToDisplayDate(display: string, years: number): string | null {
const iso = displayDateToIso(display);
if (!iso) return null;
const [year, month, day] = iso.split("-").map(Number);
const shifted = new Date(Date.UTC(year + years, month - 1, day));
return isoToDisplayDate(shifted.toISOString().slice(0, 10));
}
export function todayAsDisplayDate(): string {
return isoToDisplayDate(new Date().toISOString().slice(0, 10));
}