32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
"use client";
|
|
|
|
/**
|
|
* Renders a date/time in the browser's local timezone.
|
|
*
|
|
* Server components cannot know the visitor's timezone — `Intl` methods there
|
|
* format using the server process's timezone (typically UTC in Docker/Node),
|
|
* producing a misleading multi-hour offset. This client component guarantees
|
|
* the value is formatted in the browser's local timezone instead, matching
|
|
* client-rendered lists (e.g. `MissionCard`).
|
|
*
|
|
* Usage:
|
|
* <ClientDate date={iso} options={{ dateStyle: "medium", timeStyle: "short" }} />
|
|
* <ClientDate date={iso} options={{ weekday: "long", month: "long", day: "numeric", year: "numeric" }} />
|
|
*
|
|
* Falls back to an em dash when `date` is missing/invalid.
|
|
*/
|
|
export function ClientDate({
|
|
date,
|
|
options,
|
|
locale,
|
|
}: {
|
|
date: string | null | undefined;
|
|
options?: Intl.DateTimeFormatOptions;
|
|
locale?: string;
|
|
}) {
|
|
if (!date) return <>—</>;
|
|
const parsed = new Date(date);
|
|
if (Number.isNaN(parsed.getTime())) return <>—</>;
|
|
const formatted = new Intl.DateTimeFormat(locale ?? undefined, options).format(parsed);
|
|
return <>{formatted}</>;
|
|
}
|