Files
HealthLog/src/hooks/use-calendar-date.ts
T
Marc-André Bombeck 801295f55a fix(dates): read date-only lab and document dates as calendar dates
A date-only value is stored at 12:00 UTC. The lab list and history, the
document cards and pickers, the document date suggestion, extracted
fact dates, source-picker rows and the compliance and mood heatmaps
formatted that instant in the reader's zone, which is the next day from
UTC+12 to UTC+14.

`useCalendarDate` formats such a value through the UTC-pinned calendar
formatter the vaccination list already uses, with the same field order
and locale as instant dates. Lab readings hold both kinds, so
`useLabDate` reads a noon-UTC anchor as its calendar date and a reading
typed with its time of day in the reader's zone. A document without a
filing date shows its upload day, an instant, in the reader's zone.
2026-10-01 22:14:07 +02:00

33 lines
1.1 KiB
TypeScript

"use client";
import { useCallback } from "react";
import { formatDate } from "@/lib/date-format";
import { useDateFormatPreference, useTranslations } from "@/lib/i18n/context";
/**
* Format a date-only value: a `YYYY-MM-DD` key, or a stored instant that
* stands for a calendar date (noon UTC, or a `@db.Date` column's UTC
* midnight). The calendar date is read in UTC, so it is the same day in
* every zone.
*
* Formatting such a value in the reader's zone is how a noon-UTC date came
* to show the next day from UTC+12 to UTC+14. Same field order and locale as
* `useFormatters().date`, so it sits beside instant-formatted dates without
* looking different.
*/
export function useCalendarDate(): (value: string | Date) => string {
const { locale } = useTranslations();
const dateFormat = useDateFormatPreference();
return useCallback(
(value: string | Date) =>
formatDate(
// `formatDate` reads a bare key as a calendar date already.
typeof value === "string" ? value.slice(0, 10) : value,
dateFormat,
locale,
),
[dateFormat, locale],
);
}