Data Display
Relative Time
A hydration-safe "3 hours ago" timestamp, localized by Intl and refreshed on one shared clock.
- Deployment finished
- Comment added
- Release tagged
- Workspace created
- Trial ends
In German: . In Japanese: . Hover a time for the exact date.
Installation
Copy the source with the CLI to own and edit it, or import it from the npm package.
npx aback-ui@latest add relative-timeWrites components/ui/relative-time.tsx. First time in this project? Run npx aback-ui@latest init first, as the installation guide shows.
Usage
Import the parts you need from the installed file. The Code tab above has a complete, working example.
import { RelativeTime, formatRelativeTime } from "@/components/ui/relative-time";API reference
Props and composition points for Relative Time. Native element props pass through.
| Prop | Type | Default | Description |
|---|---|---|---|
date | Date | string | number | None | The moment to describe: a Date, an ISO string, or a timestamp. Invalid dates render nothing. Required. |
locale | string | None | BCP 47 locale for Intl. Defaults to the browser's language on the client. |
refreshInterval | number | 60000 | How often the phrase updates. Components with the same interval share one timer; 0 renders once. |
absoluteFormat | Intl.DateTimeFormatOptions | medium date, short time, UTC | The format of the server render and of the hover title. |
formatRelativeTime(date, now, locale) | function | None | The exported formatter, for places where you need the phrase as a string. |
Dependencies: no extra packages, plus the shared @/lib/utils helper.
Accessibility
Semantics, keyboard behavior, and the decisions that keep the component operable.
- It renders a time element with a machine-readable dateTime, and the exact date is the title.
- The server and the first client render show the absolute date, so cached pages hydrate without a mismatch.
- The phrase updates silently. It is not a live region, so a feed of timestamps does not chatter.
- Counts are truncated, never rounded up: 2 hours 50 minutes is "2 hours ago".
Used in production
The Aback products whose interfaces shaped this component.