Files
HealthLog/src/lib/format.ts
T
Marc-André Bombeck 38309932f3 Initial release v1.0.0
HealthLog — personal health-tracking PWA with weight, blood pressure,
pulse, mood tracking, medication compliance, Withings integration,
doctor report PDF export, and Dracula-themed UI.
2026-03-10 20:14:33 +01:00

64 lines
1.5 KiB
TypeScript

/**
* Consistent date/time formatting for the HealthLog UI.
* All functions use de-DE locale, Europe/Berlin timezone, 24h format.
*/
const TIMEZONE = "Europe/Berlin";
const LOCALE = "de-DE";
/** "19.02.2026, 14:30" */
export function formatDateTime(date: Date | string): string {
return new Date(date).toLocaleString(LOCALE, {
timeZone: TIMEZONE,
day: "2-digit",
month: "2-digit",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
hour12: false,
});
}
/** "19.02.2026" */
export function formatDate(date: Date | string): string {
return new Date(date).toLocaleDateString(LOCALE, {
timeZone: TIMEZONE,
day: "2-digit",
month: "2-digit",
year: "numeric",
});
}
/** "19.02." or "19.02.2026" if includeYear */
export function formatDateShort(
date: Date | string,
includeYear = false,
): string {
return new Date(date).toLocaleDateString(LOCALE, {
timeZone: TIMEZONE,
day: "2-digit",
month: "2-digit",
...(includeYear ? { year: "numeric" } : {}),
});
}
/** "14:30" */
export function formatTime(date: Date | string): string {
return new Date(date).toLocaleTimeString(LOCALE, {
timeZone: TIMEZONE,
hour: "2-digit",
minute: "2-digit",
hour12: false,
});
}
/** "Do., 19.02." */
export function formatDateWithWeekday(date: Date | string): string {
return new Date(date).toLocaleDateString(LOCALE, {
timeZone: TIMEZONE,
weekday: "short",
day: "2-digit",
month: "2-digit",
});
}