Shows a date as “5 minutes ago” and keeps it current. The text comes from Intl.RelativeTimeFormat, so any locale works without translation files. Hover to see the exact time.
Preview
- Build finished
- Comment added
- Backup created
- Invoice sent
- Next sync
Usage
import { RelativeTime } from "@/components/ui/relative-time"
export function LastUpdated({ at }: { at: string }) {
return (
<p className="text-sm text-muted-foreground">
Updated <RelativeTime date={at} />
</p>
)
}Examples
Locales
Pass any BCP 47 locale. The browser already knows how to say “3 days ago” in it.
endeesja
<RelativeTime date={date} locale="de" /> // vor 3 Tagen
<RelativeTime date={date} locale="ja" /> // 3 日前Stale data
With staleAfter the value gets the staleClassName and a data-stale attribute once it is older than the threshold.
Updated
Updated
Updated
Marked stale after 1 minute
<RelativeTime
date={syncedAt}
staleAfter={60_000}
updateInterval={5000}
staleClassName="text-destructive font-semibold"
/>Short format
formatStyle shortens units for tight spaces like tables.
long
short
narrow
<RelativeTime date={date} formatStyle="short" /> // 2 hr. ago
<RelativeTime date={date} formatStyle="narrow" /> // 2h agoProps
| Prop | Type | Default | Description |
|---|---|---|---|
| date | Date | string | number | - | The moment to describe |
| locale | string | "en" | Locale for Intl.RelativeTimeFormat and the tooltip |
| staleAfter | number | - | Age in ms after which the value is marked stale |
| updateInterval | number | 15000 | How often the text refreshes, in ms |
| numeric | "auto" | "always" | "auto" | “auto” gives “yesterday”, “always” gives “1 day ago” |
| formatStyle | "long" | "short" | "narrow" | "long" | Length of the unit names |
| titleFormat | Intl.DateTimeFormatOptions | { dateStyle: "medium", timeStyle: "short" } | Intl.DateTimeFormat options for the tooltip |
| staleClassName | string | "text-destructive" | Classes added when the value is stale |
| className | string | - | Additional CSS classes |
Features
- Semantic markup. Renders <time dateTime> with the ISO date, and the absolute time in title.
- No hydration mismatch. Before hydration it shows the date in UTC, then switches to the relative text.
- One timer. A hundred labels on a page share one interval through useNow.
- Future dates. Dates ahead read as “in 2 days”.