Files
EpicNext-Cms/src/lib/format-date.ts
T
openhands 9ae03056e2
CI / check (push) Successful in 1m42s
CI / deploy (push) Successful in 1m36s
CI / e2e (push) Successful in 21s
chore: remove 4 unused files (theme-editor-fields, use-user-bulk-actions, user-columns, use-media-library)
2026-09-05 23:27:02 +02:00

52 lines
1.3 KiB
TypeScript

export type DateFormatVariant =
| "date"
| "time"
| "datetime"
| "datetime-seconds";
const FALLBACKS: Record<DateFormatVariant, string> = {
date: "",
time: "—",
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
* - "time" → 18:30
* - "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 "time":
return iso.slice(11, 16);
case "datetime-seconds":
return iso.slice(0, 19).replace("T", " ");
default:
return iso.slice(0, 16).replace("T", " ");
}
}
/**
* Format a number with grouped thousands. Uses the provided locale or a
* consistent default for admin pages. Keeps number formatting in one place.
*/
export function formatNumber(n: number, locale: string = "en-US"): string {
return n.toLocaleString(locale);
}