Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

time.ts

24 lines948 Bytes

1/**
2 * Compact relative time for freshness signals ("6m ago"), measured from
3 * `now` (epoch seconds). Pages pass the instant their server function ran,
4 * so the server render and the browser's hydration print the same text.
5 */
6export function timeAgo(epochSeconds: number | null, now: number): string {
7 if (!epochSeconds) return 'never'
8 const delta = Math.max(0, now - epochSeconds)
9 if (delta < 90) return `${String(delta)}s ago`
10 if (delta < 5400) return `${String(Math.round(delta / 60))}m ago`
11 if (delta < 129_600) return `${String(Math.round(delta / 3600))}h ago`
12 return `${String(Math.round(delta / 86_400))}d ago`
14​
15/** Absolute date for provenance ("Jun 11, 2026"). */
16export function shortDate(epochSeconds: number | null): string {
17 if (!epochSeconds) return '—'
18 return new Date(epochSeconds * 1000).toLocaleDateString('en-US', {
19 month: 'short',
20 day: 'numeric',
21 year: 'numeric',
22 timeZone: 'UTC',
23 })