const dateFormat = new Intl.DateTimeFormat('en-US', { month: 'short', day: 'numeric', year: 'numeric' }) const dateTimeFormat = new Intl.DateTimeFormat('en-US', { month: 'short', day: 'numeric', year: 'numeric', hour: 'numeric', minute: '2-digit' }) const dayFormat = new Intl.DateTimeFormat('en-US', { month: 'short', day: 'numeric', year: 'numeric', timeZone: 'UTC' }) const longDate = new Intl.DateTimeFormat('en-US', { weekday: 'long', month: 'long', day: 'numeric', year: 'numeric' }) const weekLabel = new Intl.DateTimeFormat('en-US', { month: 'short', day: '2-digit', timeZone: 'UTC' }) /** `2026-10-06T14:00:00Z` → `Oct 6, 2026` in the viewer's time zone. */ export const formatDate = (iso: string) => dateFormat.format(new Date(iso)) export const formatDateTime = (iso: string) => dateTimeFormat.format(new Date(iso)) /** Calendar date without a time (`2026-10-06`) → `Oct 6, 2026`, never shifted by time zone. */ export const formatDay = (isoDate: string) => dayFormat.format(new Date(`${isoDate}T00:00:00Z`)) export const formatWeek = (isoDate: string) => weekLabel.format(new Date(`${isoDate}T00:00:00Z`)) export function toIsoDate(date: Date): string { const pad = (value: number) => String(value).padStart(2, '0') return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}` } /** Value for an `` in local time. */ export function toDateTimeLocal(date: Date): string { const pad = (value: number) => String(value).padStart(2, '0') return `${toIsoDate(date)}T${pad(date.getHours())}:${pad(date.getMinutes())}` } export const formatLongDate = (date: Date) => longDate.format(date) export function greeting(date: Date): string { const hour = date.getHours() if (hour < 12) return 'Good morning' if (hour < 18) return 'Good afternoon' return 'Good evening' } export function formatBytes(bytes: number): string { if (bytes < 1024) return `${bytes} B` if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB` return `${(bytes / 1024 / 1024).toFixed(1)} MB` }