Skip to main content
Version: 1.12.0

RelativeTimeFormatOptions

Syntax

object {
  dayOfWeek?,
  width?,
  nu?,
  context?,
  field?,
  group?,
  round?,
  minimumIntegerDigits?,
  maximumFractionDigits?,
  minimumFractionDigits?,
  maximumSignificantDigits?,
  minimumSignificantDigits?
}

Properties

  • dayOfWeek?: boolean
    • Control whether weekday names should be allowed, e.g. "2 Sundays ago". When enabled this will only emit a weekday name when the week field is being formatted and the start and end dates share the same weekday.
  • numericOnly?: boolean
    • Always use a format that includes a number, e.g. "1 day ago" instead of "Yesterday".
  • alwaysNow?: boolean
    • In numericOnly mode, if the value to be formatted is exactly zero, use the "now" format instead of a numeric format, e.g. "Today" instead of "In 0 days".
  • width?: RelativeTimeWidthType
    • Width of the unit of relative time: 'short' | 'narrow' | 'wide'
  • nu?: NumberSystemType
    • Override the numbering system
  • context?: ContextType
    • Specify the context in which the string will be display
  • field?: TimePeriodField
    • Calculate the result in terms of a single time period field
  • group?: boolean
    • Enable grouping of digits.
  • round?: RoundingModeType
    • Mode used to round numbers during formatting.
  • minimumIntegerDigits?: number
    • Minimum integer digits to display.
  • maximumFractionDigits?: number
    • Maximum fraction digits to display.
  • minimumFractionDigits?: number
    • Minimum fraction digits to display.
  • maximumSignificantDigits?: number
    • Maximum significant digits to display.
  • minimumSignificantDigits?: number
    • Minimum significant digits to display.

Defaults

{
context: 'middle-of-text'
}

Example

const cldr = framework.get('en');
const opts: RelativeTimeFormatOptions = { width: 'wide' };
const start = cldr.Calendars.toGregorianDate({ date: new Date(2019, 6, 11) });
const nums = [-5, -2, -1, 0, 1, 2, 5];
for (const n of nums) {
const end = start.add({ month: n });
let a = cldr.Calendars.formatRelativeTime(start, end, opts);
let b = cldr.Calendars.formatRelativeTime(start, end, { field: 'day', ...opts });
log(`${a} (${b})`);
}
5 months ago  (150 days ago)
2 months ago  (61 days ago)
last month  (30 days ago)
now  (today)
next month  (in 31 days)
in 2 months  (in 62 days)
in 5 months  (in 153 days)
opts = { numericOnly: true };
for (const n of nums) {
const end = start.add({ month: n });
log(cldr.Calendars.formatRelativeTime(start, end, opts));
}
5 months ago
2 months ago
1 month ago
in 0 seconds
in 1 month
in 2 months
in 5 months
nums = [-2, -1, 0, 1, 2];
opts = { numericOnly: true, alwaysNow: true };
for (const n of nums) {
const end = start.add({ month: n });
log(cldr.Calendars.formatRelativeTime(start, end, opts));
}
2 months ago
1 month ago
now
in 1 month
in 2 months
for (const n of nums) {
const end = start.add({ month: n });
log(cldr.Calendars.formatRelativeTime(start, end, { ...opts, field: 'day' }));
}
61 days ago
30 days ago
today
in 31 days
in 62 days