temporal-fns
API reference

formatRelative

API index · Input types and conventions · Installation

formatRelative(value, base, options?) → string

Label the calendar date relative to base: yesterday, today, tomorrow, last <weekday> for −6 through −2 days, and <weekday> for +2 through +6 days. Beyond that window, render MM/dd/yyyy. Date-time, zoned, and instant inputs append at and a host en-US 12-hour clock; date-only inputs do not. Clock text and spacing follow the host's Intl data.

Options are locale (only en-US) and the applicable timeZone context.

import { formatRelative } from '@frantss/temporal-fns';

const base = Temporal.PlainDate.from('2026-10-23');
formatRelative(base, base); // => 'today'
formatRelative(base.subtract({ days: 2 }), base); // => 'last Wednesday'
formatRelative(base.add({ days: 2 }), base); // => 'Sunday'
formatRelative(base.add({ days: 7 }), base); // => '10/30/2026'
const clock = Temporal.PlainDateTime.from('2026-10-23T14:30');
const clockText = clock.toPlainTime().toLocaleString('en-US', {
  hour: 'numeric',
  minute: '2-digit',
  hour12: true,
});
formatRelative(clock.add({ days: 1 }), clock) === 'tomorrow at ' + clockText; // => true

Related: format, formatDistance, formatDistanceStrict.

Behaviors: REL-010: Relative phrases select calendar dates around the base; REL-011: Date-time relative phrases include the value's clock even for distant dates; REL-012: Relative instant dates are determined in the explicit zone; REL-028: A common zone determines both the date phrase and clock of a zoned relative call.