Files
EpicNext-Cms/src/lib/format-date.ts
T
openhands b9c6001f08
Local Build and Deploy / deploy (push) Successful in 1m7s
refactor: centralize duplicated formatDate helper
21 files defined their own local formatDate (with three different output
formats: date, datetime, datetime-seconds, and varying null fallbacks).
Replace them with a single shared helper at src/lib/format-date.ts that
takes a variant + optional fallback, preserving the exact previous output
per call site (verified identical string results).

Removes ~21 copies of the same logic. photos.tsx and media-grid.tsx keep
their epoch-ms based formatters since those are a different input shape.

Verified: tsc --noEmit clean, full test suite green (301/301).
2026-07-18 19:17:17 +02:00

37 lines
992 B
TypeScript

export type DateFormatVariant = "date" | "datetime" | "datetime-seconds";
const FALLBACKS: Record<DateFormatVariant, string> = {
date: "",
datetime: "—",
"datetime-seconds": "—",
};
/**
* Format a Date for display. Kept as a single shared helper so the various
* admin/site pages no longer each redefine their own copy.
*
* - "date" → 2026-07-18
* - "datetime" → 2026-07-18 18:30
* - "datetime-seconds"→ 2026-07-18 18:30:45
*
* `fallback` overrides the default em-dash / empty string when the value is
* null/undefined.
*/
export function formatDate(
d: Date | null | undefined,
variant: DateFormatVariant = "datetime",
fallback?: string,
): string {
if (!d) return fallback ?? FALLBACKS[variant];
const iso = d.toISOString();
switch (variant) {
case "date":
return iso.slice(0, 10);
case "datetime-seconds":
return iso.slice(0, 19).replace("T", " ");
case "datetime":
default:
return iso.slice(0, 16).replace("T", " ");
}
}