Relative Time
New"3 minutes ago" that updates itself, switches to a date when old, and renders safely on the server.
- Just deployed
- Last commit
- Build started
- Previous release
- Trial ends
- Project created
Installation
$ pnpm dlx shadcn@latest add @synccn/relative-timeFirst time in this project? Connect synccn with one command.
Usage
import { RelativeTime, formatRelative } from "@/components/ui/relative-time";<RelativeTime date={comment.createdAt} />API reference
Generated from the component's TypeScript types, so it always matches the code.
RelativeTime
"3 minutes ago" that keeps itself up to date every 15 seconds. Renders the exact date on the server, so pages never hydrate with the wrong text.
- daterequired
Date | string | numberThe moment to describe.
- locale
stringDefault
"en"Locale for the wording.
- format
"long" | "short" | "narrow"Default
"long""long" says "3 minutes ago", "short" says "3 min. ago", "narrow" says "3m ago".
- absoluteAfterDays
numberPast this many days, shows the date instead ("Nov 3").
- className
stringExtra classes for the root element.
formatRelative
"3 minutes ago" / "in 2 days" for any date, using Intl.RelativeTimeFormat.
Takes no props.
Dependencies
Read from the component's imports when the site builds. The CLI installs all of these for you.
npm packages
- clsx for cn()
- tailwind-merge for cn()
Accessibility
- Uses <time> with a machine-readable datetime and the full date as a tooltip.
From the source
Read from this component's code when the site builds, so it can't drift.
No ARIA roles, key handling or motion settings in its source. It renders plain elements, so its accessibility comes from the content and labels you give it.