Time Ago
elements man recipes/time-agoA "5 minutes ago" timestamp that updates itself. A now value on the template
is reassigned every minute by a setInterval. Every binding that reads now
re-runs, so every timeAgo(date, now) cell on the page advances without
explicit refresh. The pattern is short, all client-side, and works for any UI
that shows a value that changes as time passes.
Page setup
Terminalelements create page time-ago
The page does nothing on the server beyond rendering. All the reactivity is
client-side via the template's now and the setInterval that updates it.
app/pages/time-ago/index.tsimport { Request, Response } from "@elements/app"; import timeAgo from "./template"; export default function route(req: Request, res: Response) { let serverNow = new Date(); let sampleDates = [ new Date(serverNow.getTime() - 30_000), new Date(serverNow.getTime() - 5 * 60_000), new Date(serverNow.getTime() - 2 * 60 * 60_000), new Date(serverNow.getTime() - 3 * 24 * 60 * 60_000), ]; return new timeAgo({ sampleDates }); }
The route passes a few sample timestamps anchored to the server's clock so the
demo shows different "ago" ranges. The dates are static (computed once on
render); the relative string against each one changes as the page's local now
ticks.
app/pages/time-ago/template.ehtmlimport "./style.css"; import { Table, TableHead, TableCell } from "@elements/ui/table"; function timeAgo(date: Date, now: Date): string { let seconds = Math.floor((now.getTime() - date.getTime()) / 1000); if (seconds < 5) { return "just now"; } if (seconds < 60) { return `${seconds} seconds ago`; } let minutes = Math.floor(seconds / 60); if (minutes < 60) { return minutes === 1 ? "1 minute ago" : `${minutes} minutes ago`; } let hours = Math.floor(minutes / 60); if (hours < 24) { return hours === 1 ? "1 hour ago" : `${hours} hours ago`; } let days = Math.floor(hours / 24); return days === 1 ? "1 day ago" : `${days} days ago`; } function onTick(now: { value: Date }) { now.value = new Date(); } <html class="page-time-ago" ( sampleDates: Date[], private now: { value: Date } = { value: new Date() }, ) oninit={() => setInterval(() => onTick(now), 60_000)}> <main class="page-shell is-form"> <h1>time ago</h1> <Table label="Sample timestamps"> <table> <thead> <tr> <TableHead>timestamp</TableHead> <TableHead align="end">ago</TableHead> </tr> </thead> <tbody> <tr e:for={d of sampleDates}> <TableCell><time datetime={d.toISOString()}>{d.toISOString()}</time></TableCell> <TableCell align="end">{timeAgo(d, now.value)}</TableCell> </tr> </tbody> </table> </Table> <p class="now">now: {now.value.toLocaleTimeString()}</p> </main> </html>
Three parts of the template make the labels update:
nowis template state, wrapped as{ value: Date }so theonTickhandler can reassign it from a function parameter. A plainnow: Dateparameter would only let the handler change the date's properties. To replace the whole value, the handler needs an object whose field it can assign.oniniton the template instance starts asetIntervalthat firesonTickevery 60 seconds. The interval is created once per page mount.- Each binding that reads
now.value(everyagocell and the "now: {…}" line) re-runs every timenow.valueis reassigned.timeAgois an ordinary function: each binding calls it again when the binding re-runs.
The rows are inside the Table template: a native <table> inside the styled
container, with TableHead align="end" giving the ago column tabular
figures against the inline end.
timeAgo(date, now) is a pure function: same inputs always produce the same
output. A pure function works well inside a reactive binding: the function reads two
reactive sources (d, now.value), produces a string, and the binding
rerenders whenever either changes. To add more ranges (weeks, months), add
branches to timeAgo; the template does not change.
Routes
Register the page in index.ts:
index.tsimport timeAgo from "#app/pages/time-ago"; // ... app.route("/time-ago", timeAgo);
Notes
- Tick cadence. Sixty seconds matches the resolution of "minutes ago"
labels. For an "X seconds ago" label that updates per second, switch to
setInterval(..., 1000). For sparse usage where the page is open for hours, a slower interval saves CPU at the cost of slightly stale labels. - Stopping the interval. This recipe doesn't clear the interval. Browsers
garbage-collect interval handles when the page navigates away (because the
closures referencing the template state are themselves collected). For
long-lived single-page apps that keep the template alive across many
navigations, capture the handle in
oninitand clear it inonremove. - Reactivity scope.
now.valueis reactive within the template that declared it. To share the samenowacross multiple templates, lift it to a module-level reactive value (or a singleton service) so every consumer reads the same source. The recipe keeps it per-page because the tick interval should run only when at least one timestamp is visible. - Server time vs browser time. The dates the page receives from the route
are anchored to the server's clock. The
nowthe page computes is fromnew Date()on the browser, which can drift if the user's system clock is off. For most "X ago" UIs this drift is invisible; if it matters (clinical timestamps, billing), send the server's current time with the data and use that as the base instead ofnew Date(). - Pluralization and i18n. The simple
n === 1 ? ... : ...branches intimeAgocover English. For other languages, replace the function with a call to your i18n code or useIntl.RelativeTimeFormat(browser-native, no dependency). Thenowstate and the bindings stay the same; only the string formatting changes.