Skip to content
Haze UI

Relative time

A semantic time element that shows an absolute date, then upgrades to text like '3 minutes ago' and refreshes itself.

pnpm dlx shadcn@latest add @haze/relative-time
Open preview in a new tab
Livesmall script

When to use

  • Activity feeds, comments and logs show when something happened and recency matters more than the exact date.
  • Timestamps must stay readable and correct in the HTML before any script runs.
  • Future events such as a deadline or a scheduled release need 'in 2 days' wording.

When not to use

  • The exact date and time is the point, as in invoices or audit exports; render a plain time element instead.
  • The user picks a date or time; use datetime-picker or time-picker.

Installation

Add the Haze registry to components.json once, then install Relative time with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.

pnpm dlx shadcn@latest add @haze/relative-time
First time? Registry and theme setup

1. Add to components.json:

{
  "registries": {
    "@haze": "http://localhost:4329/r/{name}.json"
  }
}

2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";

API reference

PropTypeDefaultDescription
datetimestring2026-11-21T14:05:00ZISO 8601 date-time. Keep an offset or Z so every viewer gets the same moment.
localestringenBCP 47 locale used for the absolute and relative text.
timeZonestringUTCIANA time zone for the absolute text, so server output is deterministic.
format"relative" | "absolute" | "both"relativeRelative wording, the absolute date only, or both joined as '2 hours ago · Nov 21'.
thresholdnumber–Days after which the time stays absolute instead of turning relative.
classstring–Classes for the time element.

Accessibility

  • The datetime attribute keeps the value machine-readable; the visible text is what a screen reader announces.
  • It is not a live region, so the 30-second refresh does not interrupt reading.
  • Without script the absolute date stays in place; once upgraded, the absolute date moves to the title attribute.

Notes for AI

Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.

  • Install and use

    Install the Haze UI component "Relative time" with `npx shadcn@latest add @haze/relative-time`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import RelativeTime from "@/components/haze/ui/relative-time.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/relative-time.md

  • Customize with real content

    Adapt the Haze relative-time ui for our product. Replace the demo values of these props with our real content: datetime, locale, timeZone, format, threshold, class. Keep the layout, the heading level rules and these accessibility details: The datetime attribute keeps the value machine-readable; the visible text is what a screen reader announces. It is not a live region, so the 30-second refresh does not interrupt reading. Without script the absolute date stays in place; once upgraded, the absolute date moves to the title attribute. Do not add raw color classes; use the Haze tokens.

  • Choose the right block

    Before using relative-time, check it fits: use it when activity feeds, comments and logs show when something happened and recency matters more than the exact date.; timestamps must stay readable and correct in the html before any script runs.; future events such as a deadline or a scheduled release need 'in 2 days' wording.. Avoid it when the exact date and time is the point, as in invoices or audit exports; render a plain time element instead.; the user picks a date or time; use datetime-picker or time-picker.. Alternatives: @haze/timeline, @haze/datetime-picker, @haze/time-picker.

Dependencies

  • @haze/utils
  • @haze/theme (one-time setup)

Source · Original Haze UI work.

Catalog

↑↓ moveEnter openEsc close

Keyboard shortcuts

CtrlK
Command palette
/
Focus search
←↑→↓
Move between cards
jk
Next / previous card
ci
Copy code / install command (detail page)
[]
Previous / next entry
?
This help
Esc
Close dialog