Files
blog/frontend/shared/utils/format.ts
T
2026-09-08 23:34:42 +02:00

23 lines
891 B
TypeScript

const INTL_LOCALE = { it: 'it-IT', en: 'en-GB' } as const
/** Human-readable date for display; returns an empty string when unset. */
export function formatDate(value: string | null | undefined, locale: 'it' | 'en' = 'it'): string {
if (!value) return ''
return new Intl.DateTimeFormat(INTL_LOCALE[locale], { dateStyle: 'long' }).format(new Date(value))
}
/** Date and time of publication, shown in the article byline. */
export function formatDateTime(
value: string | null | undefined,
locale: 'it' | 'en' = 'it'
): string {
if (!value) return ''
return new Intl.DateTimeFormat(INTL_LOCALE[locale], { dateStyle: 'long', timeStyle: 'short' })
.format(new Date(value))
}
/** Machine-readable date for the `datetime` attribute and structured data. */
export function isoDate(value: string | null | undefined): string {
return value ? new Date(value).toISOString() : ''
}