Relative time

Slant / Data

A time read as "12 seconds ago", kept current, and stale after a while.

Demo

Usage

@tuttn/slant
import { RelativeTime, Text } from '@tuttn/slant'

// A Date, a timestamp or an ISO string: "12 seconds ago", "in 3 days".
<Text variant="hint">Updated <RelativeTime date={run.updatedAt} /></Text>

// Stale once it is older than 10 minutes: the warning colour, and data-stale.
<RelativeTime date={captured} staleAfter={10 * 60_000} />

// Intl's own options, and a language: "1 day ago" rather than "yesterday", in Hebrew.
<RelativeTime date={due} format={{ numeric: 'always' }} locale="he" />

// No tooltip, so no tab stop either; in tests, your own clock.
<RelativeTime date={sentAt} tooltip={false} now={() => time} />

Props

PropTypeDefaultDescription
dateDate | number | string—The moment: a Date, a timestamp in milliseconds, or a string `new Date()` reads, such as an ISO date. An invalid date shows a dash.
localestringthe document's lang, else 'en'The language the time is written in, e.g. `he`. When given, it also sets the element's `lang`, so a screen reader reads the words in that language. Pass one when the page is rendered on a server.
formatIntl.RelativeTimeFormatOptions{ numeric: 'auto', style: 'long' }How `Intl.RelativeTimeFormat` writes the time, laid over the default: `numeric: 'always'` says "1 day ago" where the default says "yesterday", and `style: 'short'` says "12 sec. ago".
staleAfternumber—Milliseconds after which the time reads as stale: once it is older than this, the text turns the warning colour and the element gets `data-stale`.
tooltipbooleantrueShows the full date and time in a Tooltip on hover and keyboard focus, and makes the time focusable so keyboard users can reach it.
now() => number—The clock, in milliseconds since the epoch. Pass your own to drive it in tests or to sync with a server's time.
…restHTMLAttributes<HTMLTimeElement>—The ref, className, style and other attributes go on the <time>. The tooltip is a separate element in a portal.

Keyboard

Tabreaches the time (with tooltip, the default), which shows the full date
Escapehides the full date; focus stays on the time

Accessibility

  • Renders a native <time> whose dateTime is the ISO date, so the moment is machine-readable whatever the words say.
  • The full date is a Tooltip that describes the time (aria-describedby), with no title, so there is never a second, native tooltip. The time is a tab stop only while it has the tooltip, so keyboard users reach the date too.
  • The words change as the time ages, but the time is not a live region, so the ticking is never read out. A screen reader reads the current words when it reaches them.
  • Stale is shown in the warning colour, which holds 4.5:1 in every theme. The words already say how old the time is; where staleness matters, say so in words beside it too.
  • With a locale, the time takes a matching lang, so a screen reader reads its words in that language.

Words

The text comes from Intl.RelativeTimeFormat, in the largest unit that fits, rounded to the nearest whole one: seconds under a minute, then minutes, hours, days, weeks, months and years. A count that rounds up to the next unit reads as one of it, so 59.6 seconds is "1 minute ago", never "60 seconds ago". The default numeric: 'auto' says "now", "yesterday" and "next month"; format is laid over it, so { style: 'short' } keeps those words and { numeric: 'always' } says "1 day ago". The language is locale, else the document's lang, else English.

Ticking

Every RelativeTime on the page shares one clock per step, so a hundred of them tick in the same frame on one timer. A time under a minute away renders every second, one under an hour every minute, and anything further every hour; the steps fall on the clock's whole seconds, minutes and hours, and they stop while the page is hidden and catch up when it shows. Near staleAfter it ticks every second, so it turns stale on time. now swaps the clock it reads, to drive it in tests or to follow a server's time.

Stale

Once the time is older than staleAfter milliseconds, the text turns the warning colour and the element gets data-stale, which your own styles can use too. A time in the future is never stale.

Server rendering

There is no clock on the server or during hydration, so a RelativeTime writes the absolute date then ("Oct 31, 2026, 9:30 AM") and switches to relative words once the page hydrates. The server writes that date in its own time zone and the browser in the reader's; the time allows for the difference, so hydration doesn't warn. Pass locale when you render on a server, so both write the same language.

Invalid dates

A date that new Date() can't read shows a dash, with no dateTime, no tooltip and no tab stop.