Data display
RelativeTime
Times such as “5 minutes ago” or “in 3 days”, in the reader’s language, inside a real <time> element with the exact date as its title.
import { RelativeTime } from '@reactloom/ui';Examples
Past and future
live updates the text as time passes (every few seconds for recent times, less often later).
Edited
Comment posted
Deployed
Trial ends
import { RelativeTime, Stack, Text } from '@reactloom/ui';
const now = Date.now();
const minute = 60_000;
export default function Example() {
return (
<Stack gap="2">
<Text>
Edited <RelativeTime date={now - 30 * 1000} live />
</Text>
<Text>
Comment posted <RelativeTime date={now - 5 * minute} live />
</Text>
<Text>
Deployed <RelativeTime date={now - 26 * 60 * minute} />
</Text>
<Text>
Trial ends <RelativeTime date={now + 3 * 24 * 60 * minute} />
</Text>
</Stack>
);
}Style and threshold
Older dates switch to an absolute date after threshold milliseconds.
Short style:
Past the threshold, the date is shown:
import { RelativeTime, Stack, Text } from '@reactloom/ui';
const day = 24 * 60 * 60_000;
const lastYear = Date.UTC(2025, 2, 14, 9, 30);
export default function Example() {
return (
<Stack gap="2">
<Text>
Short style: <RelativeTime date={Date.now() - 2 * day} unitStyle="short" />
</Text>
<Text>
Past the threshold, the date is shown: <RelativeTime date={lastYear} threshold={7 * day} format={{ dateStyle: 'medium' }} />
</Text>
</Stack>
);
}API reference
| Prop | Type | Default |
|---|---|---|
daterequiredThe moment to describe. | Date | number | string | — |
liveRe-render as time passes. | boolean | false |
thresholdBeyond this many milliseconds, show the absolute date instead. | number | — |
formatFormat of the absolute date (title and past the threshold). | Intl.DateTimeFormatOptions | { dateStyle: 'medium', timeStyle: 'short' } |
numeric“1 day ago” or “yesterday”. | 'always' | 'auto' | 'auto' |
unitStyleUnit length. | 'long' | 'short' | 'narrow' | 'long' |
Customization
Extracted from the component’s source on every build, so it matches the code. How the layers fit together is explained in Customization.
Accessibility
- Renders
<time dateTime>; the exact date is available as its title. - The server renders the absolute date, and the relative text replaces it after hydration, so no mismatch occurs.
- Live updates are not announced; put changing times outside live regions.
Usage guidelines
Do
- Show the exact date nearby or in a tooltip when precision matters (invoices, audit logs).
Don’t
- Don’t use relative times for dates people must compare or copy.