FormatRelativeTime
Display time relative to now
import FormatRelativeTime from "@intility/bifrost-react/FormatRelativeTime";useRelativeTime()string.<FormatDate>Relative time formatting
Pass in a Date
object
to date prop to display a relative time string (e.g., "5 minutes ago", "in 2
days"), including a tooltip on hover with the full date and time.
The component automatically chooses the most appropriate unit (seconds, minutes, hours, days, months, or years) based on the time difference:
<FormatRelativeTime date={myDate} />
The component will automatically re-render to keep the displayed relative time up to date as time passes. For example;
- "5 minutes ago" will update to "6 minutes ago" after one minute
- "1 second ago" will update to "2 seconds ago" after one second
Locale
FormatRelativeTime follows current bifrost locale (default english).
Use <Bifrost locale={nbNO}> in your app to use norwegian
formatting.
Norwegian
<Bifrost locale={nbNO}> <FormatRelativeTime date={myDate} /> </Bifrost>
Swedish
<Bifrost locale={svSE}> <FormatRelativeTime date={myDate} /> </Bifrost>
Hide tooltip
By default, the component shows a tooltip on hover with the full date and time
(including month name, time, and timezone) to provide context for the relative
time. If you don't want a tooltip, you can hide it with the noTooltip prop:
<FormatRelativeTime date={myDate} noTooltip />
Sandbox
import FormatRelativeTime from "@intility/bifrost-react/FormatRelativeTime"; import Grid from "@intility/bifrost-react/Grid"; export default function FormatRelativeTimeDemo() { const now = new Date(); const janFirst2026ISOString = "2026-01-01T12:00:00Z"; // calculations for demo purposes only; // in a real app, you would use a fixed date from your data source instead const fiveMinutesAgo = new Date(now.getTime() - 5 * 60 * 1000); const twoHoursAgo = new Date(now.getTime() - 2 * 60 * 60 * 1000); const threeDaysAgo = new Date(now.getTime() - 3 * 24 * 60 * 60 * 1000); const inTwoDays = new Date(now.getTime() + 2 * 24 * 60 * 60 * 1000); return ( <Grid> <div> page loaded: <FormatRelativeTime date={now} /> </div> <div> page loaded (no tooltip): <FormatRelativeTime date={now} noTooltip /> </div> <div> Jan 1, 2026 string: <FormatRelativeTime date={janFirst2026ISOString} /> </div> <div> equivalent date object: <FormatRelativeTime date={new Date(janFirst2026ISOString)} /> </div> <FormatRelativeTime date={fiveMinutesAgo} /> <FormatRelativeTime date={twoHoursAgo} /> <FormatRelativeTime date={threeDaysAgo} /> <FormatRelativeTime date={inTwoDays} /> </Grid> ); }