2026-09-11 13:52:13 +02:00
|
|
|
import type { Locale } from './locale'
|
|
|
|
|
|
|
|
|
|
const INTL_LOCALE: Record<Locale, string> = { it: 'it-IT', en: 'en-GB' }
|
|
|
|
|
|
2026-08-25 11:42:17 +02:00
|
|
|
/** Human-readable date for display; returns an empty string when unset. */
|
2026-09-11 13:52:13 +02:00
|
|
|
export function formatDate(value: string | null | undefined, locale: Locale = 'it'): string {
|
2026-08-25 11:42:17 +02:00
|
|
|
if (!value) return ''
|
2026-09-11 13:52:13 +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-11 13:52:13 +02:00
|
|
|
export function formatDateTime(value: string | null | undefined, locale: Locale = 'it'): string {
|
2026-08-25 15:30:57 +02:00
|
|
|
if (!value) return ''
|
2026-09-11 13:52:13 +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() : ''
|
|
|
|
|
}
|