Agent Wranglers

APP-14Optional

Show relative times with the exact time beside them

Put each relative time, such as "5 minutes ago", in a <time> element with the exact time in datetime. Show the exact local time in a tooltip on hover and on keyboard focus.

How to do it

Format the words with Intl.RelativeTimeFormat or a library such as Babel, never by joining strings, so plurals come out right. Switch to an exact date after 7 days. Refresh relative times once a minute while the tab is visible. Never use relative times in exports, emails or audit logs. A title attribute shows on hover only, so give the <time> tabindex="0" and use the shared tooltip component.

Examples

TypeScript example

const UNITS: [Intl.RelativeTimeFormatUnit, number][] = [['day', 86_400], ['hour', 3_600], ['minute', 60], ['second', 1]];

export function relativeTime(isoUtc: string, locale: string, now = Date.now()): string {
  const seconds = Math.round((Date.parse(isoUtc) - now) / 1000);
  const [unit, size] = UNITS.find(([, size]) => Math.abs(seconds) >= size) ?? UNITS.at(-1)!;
  return new Intl.RelativeTimeFormat(locale, { numeric: 'auto' }).format(Math.round(seconds / size), unit);
}

Python example

from datetime import datetime, timezone
from babel.dates import format_timedelta

def relative_time(moment: datetime, locale: str) -> str:
    return format_timedelta(moment - datetime.now(timezone.utc), add_direction=True, locale=locale)

HTML example

<time datetime="2026-11-02T09:15:04Z" tabindex="0" aria-describedby="time-1042">5 minutes ago</time>
<span role="tooltip" id="time-1042" class="ui-tooltip">2 Nov 2026, 10:15 CET</span>

How to check

A component test renders a time from 90 seconds ago. It shows "2 minutes ago" or "1 minute ago", and the <time> element carries the full ISO time. Focusing it by keyboard shows the exact local time.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/app-ui/dates-and-numbers/relative-times">Agent Wranglers web app interface playbook</a>. In code you write, add a comment next to it with the same link.