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

@sumup-oss/intl / formatRelativeTimeToParts

Variable: formatRelativeTimeToParts

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

Defined in: lib/relative-time-format/index.ts:109

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

Parameters

Parameter Type Description
value number -
unit RelativeTimeFormatUnit -
locales? string | string[] Formatting locale. Omitting this is deprecated and will become required in a future major version. When omitted, formatting uses the runtime default (browser or server locale), which may not match your user.
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.