GooseUI

Components

Relative Time

New

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

Installation

npx shadcn@latest add @gooseui/relative-time
pnpm dlx shadcn@latest add @gooseui/relative-time
yarn dlx shadcn@latest add @gooseui/relative-time
bunx --bun shadcn@latest add @gooseui/relative-time

The component refreshes itself with the useNow hook, which is installed along with it.

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 ago

Props

PropTypeDefaultDescription
dateDate | string | number-The moment to describe
localestring"en"Locale for Intl.RelativeTimeFormat and the tooltip
staleAfternumber-Age in ms after which the value is marked stale
updateIntervalnumber15000How 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
titleFormatIntl.DateTimeFormatOptions{ dateStyle: "medium", timeStyle: "short" }Intl.DateTimeFormat options for the tooltip
staleClassNamestring"text-destructive"Classes added when the value is stale
classNamestring-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”.