-
Notifications
You must be signed in to change notification settings - Fork 4
Variable.formatRelativeTimeToParts
connor-baer edited this page Oct 23, 2025
·
32 revisions
@sumup-oss/intl / formatRelativeTimeToParts
constformatRelativeTimeToParts: (value,unit,locales?,options?) =>RelativeTimeFormatPart[]
Defined in: lib/relative-time-format/index.ts:97
Formats a relative time to parts with support for various styles.
| Parameter | Type |
|---|---|
value |
number |
unit |
RelativeTimeFormatUnit |
locales? |
string | string[] |
options? |
RelativeTimeFormatOptions |
RelativeTimeFormatPart[]
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" }
// ]In runtimes that don't support the Intl.RelativeTimeFormat API,
the relative time is formatted using the Intl.NumberFormat API instead.