Function.formatRelativeTimeToParts - sumup-oss/intl-js GitHub Wiki

@sumup-oss/intl / formatRelativeTimeToParts

Function: formatRelativeTimeToParts()

formatRelativeTimeToParts(value, unit, locales?, options?): RelativeTimeFormatPart[]

Formats a relative time to parts with support for various styles.

Parameters

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

Returns

RelativeTimeFormatPart[]

Example

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

formatRelativeTimeToParts(1, 'day', 'de-DE');
// [
//  { "type": "literal", "value": "in " },
//  { "type": "integer", "unit": "day", "value": "1" },
//  { "type": "literal", "value": " Tag" }
// ]
formatRelativeTimeToParts(7, 'years', ['pt-BR', 'pt']);
// [
//   { "type": "literal", "value": "em " },
//   { "type": "integer", "unit": "year", "value": "7" },
//   { "type": "literal", "value": " anos" }
// ]
formatRelativeTimeToParts(-5, 'months', 'en-GB', {
  style: 'narrow',
});
// [
//   { "type": "integer", "unit": "month", "value": "5" },
//   { "type": "literal", "value": " 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:97