Files
faunosteo/apps/app/src/lib/dates.ts
T

30 lines
817 B
TypeScript

import type { Locale } from '@/i18n/types'
export function todayIso(): string {
return new Date().toISOString().slice(0, 10)
}
export function nowIso(): string {
return new Date().toISOString()
}
const LOCALE_TAG: Record<Locale, string> = {
fr: 'fr-FR',
en: 'en-US',
}
export function formatDate(isoDate: string, locale: Locale): string {
const match = isoDate.match(/^(\d{4})-(\d{2})-(\d{2})$/)
if (!match) throw new Error(`Invalid ISO date: ${isoDate}`)
const [, y, mo, d] = match
const date = new Date(Date.UTC(Number(y), Number(mo) - 1, Number(d)))
if (Number.isNaN(date.getTime()))
throw new Error(`Invalid ISO date: ${isoDate}`)
return new Intl.DateTimeFormat(LOCALE_TAG[locale], {
day: 'numeric',
month: 'long',
year: 'numeric',
timeZone: 'UTC',
}).format(date)
}