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