Skip to content

Function.formatRelativeTime

connor-baer edited this page Dec 5, 2024 · 3 revisions

@sumup-oss/intl / formatRelativeTime

Function: formatRelativeTime()

formatRelativeTime(value, unit, locales?, options?): string

Formats a relative time with support for various styles.

Parameters

Parameter Type
value number
unit RelativeTimeFormatUnit
locales? string | string[]
options? RelativeTimeFormatOptions

Returns

string

Example

import { formatRelativeTime } from '@sumup-oss/intl';

formatRelativeTime(1, 'day', 'de-DE'); // 'in 1 Tag'
formatRelativeTime(7, 'years', ['pt-BR', 'pt']); // 'em 7 anos'
formatRelativeTime(-5, 'months', 'en-GB', {
  style: 'narrow',
}); // '5 mo ago'

Remarks

In runtimes that don't support the Intl.RelativeTimeFormat API, the relative time is formatted using the Intl.NumberFormat API instead.

Defined in

lib/relative-time-format/index.ts:44