Files
crosscode-platform/apps/events/frontend/src/lib/date.ts
T
joshua fdd2d241b4 Initial commit
Next.js + Express event management app for Hope Family Church.
2026-07-23 15:26:47 +02:00

38 lines
1.1 KiB
TypeScript

export function formatDate(input: string | number | Date | null | undefined): string {
if (input == null) return "";
const d = new Date(input);
if (isNaN(d.getTime())) return "";
// Use en-GB to ensure day-month-year with full month name, e.g., 20 March 2025
return new Intl.DateTimeFormat("en-GB", {
day: "2-digit",
month: "long",
year: "numeric",
}).format(d);
}
export function formatDateTime(input: string | number | Date | null | undefined): string {
if (input == null) return "";
const d = new Date(input);
if (isNaN(d.getTime())) return "";
return new Intl.DateTimeFormat("en-GB", {
day: "2-digit",
month: "long",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
hour12: false,
}).format(d);
}
export function formatDateTimeRange(
start: string | number | Date | null | undefined,
end: string | number | Date | null | undefined
): string {
const startStr = formatDateTime(start);
const endStr = formatDateTime(end);
if (startStr && endStr) return `${startStr} — ${endStr}`;
if (startStr) return startStr;
if (endStr) return endStr;
return "";
}