Skip to content

Kody is live

Watch the launch video — what Kody is, and why it exists.

← Public packages

@kentcdodds/package-app-kit

Design tokens, PWA install/update, About/version, cache helpers, and optional realtime notes sync for Kody package apps.

app/ui/local-time.ts

38 lines · 1.0 KB · TypeScript
/** Format an ISO timestamp in the browser's local timezone. */
export function formatLocalDateTime(iso: string | null | undefined): string {
	const raw = String(iso || '').trim()
	if (!raw) return '—'
	const ms = Date.parse(raw)
	if (!Number.isFinite(ms)) return '—'
	try {
		return new Intl.DateTimeFormat(undefined, {
			dateStyle: 'medium',
			timeStyle: 'short',
		}).format(new Date(ms))
	} catch {
		return new Date(ms).toLocaleString()
	}
}

export function lastUpdateCheckStorageKey(appId = 'package-app-kit-demo'): string {
	return `${appId}-last-update-check`
}

export function readLastUpdateCheckAt(appId?: string): string | null {
	if (typeof localStorage === 'undefined') return null
	try {
		const value = localStorage.getItem(lastUpdateCheckStorageKey(appId))
		return value && Date.parse(value) ? value : null
	} catch {
		return null
	}
}

export function writeLastUpdateCheckAt(iso = new Date().toISOString(), appId?: string): string {
	try {
		localStorage.setItem(lastUpdateCheckStorageKey(appId), iso)
	} catch {
		/* ignore */
	}
	return iso
}