<!-- generated by scripts/gen-docs.mjs from docs/system/relative-time.md. Edit the source and rerun. -->
# RelativeTime

## Description
Says when something happened the way people say it, "5 minutes ago" or "yesterday", and from seven days on gives the date, with the full date and time on hover.

`import { RelativeTime } from "@/components/cobalt/ui/relative-time"`, source `registry/cobalt/ui/relative-time.tsx`, status `ready`.
Foundation: `hand-rolled`. Traps checked: `trap/numbers-shift`, `trap/empty-value`, `trap/demo-on-timers`, `trap/loading-layout-shift`.

`trap/numbers-shift` is answered by tabular figures, so "9 minutes" to "10 minutes" moves only by the added digit (Tokens). `trap/empty-value` by `none`, "Unknown", for a value that is not a date (States). `trap/demo-on-timers` by `now`, which freezes the text for tests and stress pages, and by examples that count from their first render (Rules). `trap/loading-layout-shift` by `whitespace-nowrap`, so a tick never wraps the text onto a second line (Limits).

### Foundation
Not applicable: hand-rolled.

| Given by the foundation | Added by the team |
|---|---|
| Not applicable: hand-rolled, the HTML `time` and `Intl.RelativeTimeFormat` | `relativeTime()` in `registry/cobalt/lib/time-ago.ts`: under a minute "now", then minutes, then hours, then calendar days ("yesterday"), and from 7 days the short date, with the year only when it is another year; a time under 6 hours ago stays in hours across midnight; the full date and time in the system tooltip on hover, never the browser's `title`, and the instant in `dateTime`; a timer that ticks every 15 seconds in the first minute, every minute within the hour, every hour after, and never once the text is a date; `suppressHydrationWarning` for the server and client clocks; `locale` and `timeZone`; "Unknown" for a broken timestamp; `focusable` for a time that stands alone: a tab stop whose tooltip opens on keyboard focus, toggles on a tap and closes on a tap elsewhere or Escape, the full date read after the words through `aria-describedby` from a hidden span, and a 4px corner (`--r-xs`) for its focus outline (`registry/cobalt/ui/relative-time.tsx:81-118`) |

## Examples
Times from a minute to a month ago; hover one for the full date.

Real uses, 0 call sites (`rg -n "<RelativeTime\b" app registry --glob '!registry/cobalt/ui/*'`): the part is new. TSA formats "posted 3 days ago" by hand for four screens (`docs/research/2026-10-17-coverage.md`, section 3).

Uses inside the registry, which other parts build on:
- SourceLine's "Updated 5 minutes ago", the one `focusable` use, with SourceLine's own words for a time it cannot read (`registry/cobalt/ui/source-line.tsx:117`).
- Inbox's time at the end of each notice (`registry/cobalt/ui/inbox.tsx:299`).
- In other parts' examples: Timeline in a card, ListDetail's list rows and the page layout's filings (`rg -n "<RelativeTime\b" docs/system/examples --glob '!docs/system/examples/relative-time/*'`).

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/relative-time/default.tsx` | default | Times from a minute to a month ago; hover one for the full date |
| `docs/system/examples/relative-time/state-relative.tsx` | state:relative | Under a week it is words, and it keeps itself current, so "now" becomes "1 minute ago" as you watch |
| `docs/system/examples/relative-time/state-absolute.tsx` | state:absolute | From seven days on it is the date, with the year when it is not this year |
| `docs/system/examples/relative-time/state-unknown.tsx` | state:unknown | A timestamp that is not a date, as an import can leave it, reads Unknown in the time's own place |
| `docs/system/examples/relative-time/state-frozen.tsx` | state:frozen | With now passed, the text counts from that moment and never ticks, as a test or a screenshot needs |
| `docs/system/examples/relative-time/in-item.tsx` | composition:Item | The time at the end of a list row, in the row's small muted figures |
| Not applicable: the one `focusable` use is SourceLine's, in `docs/system/examples/source-line/default.tsx` | state:focusable | A time that stands alone takes focus, and its tooltip opens on focus and on a tap |

Example: Times from a minute to a month ago; hover one for the full date (`docs/system/examples/relative-time/default.tsx`)

```tsx
import { useState } from "react";

import { RelativeTime } from "@/components/cobalt/ui/relative-time";

const ago = (minutes: number) => Date.now() - minutes * 60_000;

export default function Example() {
  const [times] = useState(() => [
    { what: "Comment from Jo Park", at: ago(0.2) },
    { what: "Booking changed", at: ago(12) },
    { what: "Payment received", at: ago(60 * 5) },
    { what: "Booking made", at: ago(60 * 26) },
    { what: "Account opened", at: ago(60 * 24 * 40) },
  ]);
  return (
    <ul className="flex max-w-sm flex-col gap-2 text-sm">
      {times.map((t) => (
        <li key={t.what} className="flex items-baseline justify-between gap-4">
          <span>{t.what}</span>
          <RelativeTime value={t.at} className="text-xs text-muted-foreground" />
        </li>
      ))}
    </ul>
  );
}
```

Example: The time at the end of a list row, in the row's small muted figures (`docs/system/examples/relative-time/in-item.tsx`)

```tsx
import { useState } from "react";

import { Item, ItemActions, ItemContent, ItemDescription, ItemGroup, ItemTitle } from "@/components/cobalt/ui/item";
import { RelativeTime } from "@/components/cobalt/ui/relative-time";

export default function Example() {
  const [rows] = useState(() => [
    { title: "Harbour Large Cap Core", about: "Filed a 13F", at: Date.now() - 3 * 3_600_000 },
    { title: "Calder Global Equity", about: "Amended its Form ADV", at: Date.now() - 50 * 3_600_000 },
  ]);
  return (
    <ItemGroup variant="card" className="max-w-md">
      {rows.map((r) => (
        <Item key={r.title}>
          <ItemContent>
            <ItemTitle>{r.title}</ItemTitle>
            <ItemDescription>{r.about}</ItemDescription>
          </ItemContent>
          <ItemActions>
            <RelativeTime value={r.at} />
          </ItemActions>
        </Item>
      ))}
    </ItemGroup>
  );
}
```

## Variants
Not applicable: RelativeTime has one look; the host sets its size and colour, such as `text-xs text-muted-foreground` in a row.

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| relative | the value is less than 7 calendar days from now, either way | read it; hover for the full date | words such as "5 minutes ago", `data-relative` | test |
| absolute | the value is 7 or more days from now | read it; hover for the full date and time | the short date, such as "Sep 30" or "Dec 31, 2025" | test |
| unknown | the value is empty, null, or not a date | read that the time is unknown | the word "Unknown", or the `none` text, with no `time` element | test |
| frozen | `now` is passed | read it | the same text on every render; no timer runs | test |
| focusable | `focusable` is passed | Tab to it, tap it, read the full date | the tooltip on keyboard focus and on a tap, the outline at a 4px corner, and the full date after the words for a screen reader | test |

Example: Under a week it is words, and it keeps itself current, so "now" becomes "1 minute ago" as you watch (`docs/system/examples/relative-time/state-relative.tsx`)

```tsx
import { useState } from "react";

import { RelativeTime } from "@/components/cobalt/ui/relative-time";

export default function Example() {
  const [at] = useState(() => Date.now() - 40_000);
  return (
    <p className="text-sm">
      Saved <RelativeTime value={at} />
    </p>
  );
}
```

Example: From seven days on it is the date, with the year when it is not this year (`docs/system/examples/relative-time/state-absolute.tsx`)

```tsx
import { RelativeTime } from "@/components/cobalt/ui/relative-time";

// Pinned, so the first date stays in this year and the caption stays true next year.
const now = new Date("2026-10-17T14:55:00Z");

export default function Example() {
  return (
    <ul className="flex flex-col gap-2 text-sm">
      <li>
        Filed <RelativeTime value="2026-09-30T14:20:00Z" now={now} />
      </li>
      <li>
        First filed <RelativeTime value="2014-03-03T09:00:00Z" now={now} />
      </li>
    </ul>
  );
}
```

Example: A timestamp that is not a date, as an import can leave it, reads Unknown in the time's own place (`docs/system/examples/relative-time/state-unknown.tsx`)

```tsx
import { RelativeTime } from "@/components/cobalt/ui/relative-time";

export default function Example() {
  return (
    <ul className="flex w-full max-w-sm flex-col gap-2 text-sm">
      <li className="flex items-baseline justify-between gap-4">
        <span>Holdings last synced</span>
        <RelativeTime value="0000-00-00 00:00:00" className="text-xs text-muted-foreground" />
      </li>
    </ul>
  );
}
```

Example: With now passed, the text counts from that moment and never ticks, as a test or a screenshot needs (`docs/system/examples/relative-time/state-frozen.tsx`)

```tsx
import { RelativeTime } from "@/components/cobalt/ui/relative-time";

// The moment a test or a stress page pins, so the words are the same on every run.
const now = new Date("2026-10-17T14:55:00Z");

export default function Example() {
  return (
    <p className="text-sm">
      Filed <RelativeTime value="2026-10-17T09:40:00Z" now={now} timeZone="UTC" />
    </p>
  );
}
```

### State precedence
- Relative and absolute: Not applicable: the 7-day line puts a value on one side.
- Relative and frozen: frozen wins. The text is counted from `now` and never ticks.
- Unknown and every other state: unknown wins. There is no instant to count from, so there is no tooltip and no tab stop.
- Focusable and relative, absolute or frozen: both show. The words follow their state, and the tooltip holds the full date either way.

### Motion
The words change in place when a tick changes them; nothing moves, since the text never wraps and its figures are tabular.

| Trigger | Kind | Preset | Properties | Reduced motion |
|---|---|---|---|---|
| hover, or keyboard focus or a tap on a `focusable` time, to the tooltip and back | announce | Tooltip's `popup-motion`, `--t-press` on `--curve`, leaving in 70 percent of it | opacity | the same fade; nothing moves |

## Props

<!-- Props generated from registry/cobalt/ui/relative-time.tsx with TypeScript by scripts/props-table.mjs -->
| Prop | Type | Default | Purpose |
|---|---|---|---|
| `value` | `string \| number \| Date` |  | The instant: a Date, an ISO 8601 string or milliseconds since 1970. |
| `focusable?` | `boolean` | `false` | Makes the time a tab stop: keyboard focus opens its tooltip, a tap toggles it on a touch screen, and a screen reader hears the full date after the words. |
| `locale?` | `string` |  |  |
| `none?` | `string` | `"Unknown"` | What a value that is not a date reads. |
| `now?` | `number \| Date` |  | The moment to count from. |
| `timeZone?` | `string` |  | The time zone for calendar days and the full date. |

Also accepts every native `<time>` attribute.

`value` is a Date, an ISO 8601 string or milliseconds. `now` freezes the moment counted from. `locale` and `timeZone` set the language and the calendar day; both default to the reader's. `none` is the text for a value that is not a date. `focusable` makes the time a tab stop for a time that stands alone, such as SourceLine's "Updated 5 minutes ago": keyboard focus opens its tooltip, a tap toggles it on a touch screen and a tap elsewhere or Escape closes it, and a screen reader hears the full date after the words through `aria-describedby`; leave it off in a row or a list, where a tab stop per time would be noise. Every native `time` attribute passes through except `dateTime`, which the component sets.

## Usage

### When to use
- A row, a comment or an event shows when it happened, and recent times matter more than exact ones.
- A status line says when something was last saved or synced.

### When not to use
- The exact date is the fact, such as a filing date in a table or a due date. Use DataList instead, with the date from `date()` in `@/lib/cobalt/format`.
- The time is in the future and the hour matters, such as a check-in. Use Table instead, or a plain `time` with the date and hour.

### Rules
- `rule/relative-time-freeze-in-tests`: When rendering `RelativeTime` in a test, a stress page or a screenshot, pass `now`, because the text otherwise follows the clock and a snapshot taken a minute later fails. Evidence: principle heuristic: consistency and standards, a test sees the same page twice; single use docs/system/stress/relative-time.tsx:45. Check: test `relative-time.test.tsx` passes `now` in every case.
  - Don't: `renderToStaticMarkup(<RelativeTime value="2026-10-17T14:55:00Z" />)`
  - Do: `renderToStaticMarkup(<RelativeTime value="2026-10-17T14:55:00Z" now={now} timeZone="UTC" />)`
- `rule/relative-time-pass-the-instant`: When the source sends a timestamp, pass it as it came, an ISO 8601 string or milliseconds, as `value`, instead of a string you formatted first, because the component needs the instant to count from and to put in `dateTime` and `title`. Evidence: principle platform: `Intl.RelativeTimeFormat` and the `time` element both take the instant; principle heuristic: error prevention. Check: review.
  - Don't: `<RelativeTime value={date(filing.filed)} />`
  - Do: `<RelativeTime value={filing.filedAt} />`

### Content
- `rule/relative-time-no-prefix-word`: When a label sits before `RelativeTime`, use a verb in the past tense, such as "Saved" or "Filed", instead of "Last updated:", because the time reads as the end of a sentence, "Saved 5 minutes ago". Evidence: principle heuristic: match between system and the real world; app 2/2 source apps read times as part of a sentence, docs/research/2026-10-17-coverage.md section 3. Check: review.
  - Don't: `Last updated: <RelativeTime value={at} />`
  - Do: `Saved <RelativeTime value={at} />`

### Anti-slop
- `rule/relative-time-no-tooltip`: When the full date should be readable, rely on the tooltip the component draws, with `focusable` where the time stands alone, instead of wrapping it in a `Tooltip` of your own, because a second tooltip on text that is not a control has no keyboard path and adds a second popup to a row. Evidence: principle wcag: 1.4.13 Content on hover or focus; person D36. Check: review.
  - Don't: `<Tooltip><TooltipTrigger><RelativeTime value={at} /></TooltipTrigger><TooltipContent>{full}</TooltipContent></Tooltip>`
  - Do: `<RelativeTime value={at} />`

### Limits
- `rule/relative-time-column-width`: When a column holds relative times, give it at least 80px, because the longest text measured, "vor 3 Stunden" in German, is 77px at 12px and the text never wraps. Evidence: measured 77px for "vor 3 Stunden", 74px for "Dec 31, 2099" and 54px for "in 3 hours" at `text-xs`, docs/system/stress/relative-time.tsx. Check: probe docs/system/stress/relative-time.tsx on the break-ui harness at 320.
  - Don't: `<span className="w-12 truncate"><RelativeTime value={at} /></span>`
  - Do: `<span className="min-w-20 shrink-0"><RelativeTime value={at} /></span>`

## Accessibility
Rests on the HTML `time`.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Tab | before the time | passes over it; the text is not focusable unless `focusable` is passed | the next focusable element | by hand |
| Tab | before a `focusable` time | focuses it and opens its tooltip; Escape closes it | the time (measured in SourceLine) | by hand |
| a tap | a `focusable` time on a touch screen | opens its tooltip, and a second tap or a tap elsewhere closes it | the time | by hand |

### ARIA
| Part | Role | Accessible name from | States and properties | Announced | Checked by |
|---|---|---|---|---|---|
| root | time | its text | `dateTime` with the instant; the full date in the tooltip; with `focusable`, `tabindex="0"` and `aria-describedby` a hidden span with the full date | the words, such as "5 minutes ago"; a tick is not announced | test |
| unknown | none, a `span` | its text | none | "Unknown" | test |

## Tokens
| Part | State | Token |
|---|---|---|
| root | all | `--font`, inherited colour from the host |
| root | focusable, focus-visible | `--ring`, `--r-xs` |
| root | unknown | `--muted-foreground` |
| tooltip | open | Tooltip's: `--tip`, `--tip-foreground`, `--edge`, `--t-press`, `--curve` |

## Related
- Timeline: events with their times.
- Item: a row with its time at the end, or its date as a leaf at the start.
- DataList: a fact whose exact date matters.
- SourceLine: names where figures came from and when they were updated, with a focusable RelativeTime.
- Tooltip: the full date's surface.
