# General Translation Platform: formatRelativeTime URL: https://generaltranslation.com/en-GB/docs/platform/core/reference/gt-class-methods/formatting/format-relative-time.mdx --- title: formatRelativeTime description: Format relative time values such as minutes ago or days from now. API reference for formatRelativeTime. --- Formats a relative time value with an explicit unit according to locale-specific conventions, on a [GT](/docs/platform/core/reference/gt-class/constructor) instance. General Translation uses the built-in `Intl.RelativeTimeFormat` API to produce phrases such as "2 hours ago" or "in 3 days". ## Overview [#overview] Call `formatRelativeTime` on a [`GT`](/docs/platform/core/reference/gt-class/constructor) instance with a numeric value, a time unit, and an optional options object. A negative value is in the past, a positive value is in the future. It returns the formatted string. ```typescript const gt = new GT(); const formatted = gt.formatRelativeTime(-1, 'day', { locales: 'en-US', numeric: 'auto', }); // "yesterday" ``` Signature: ```typescript formatRelativeTime( value: number, unit: Intl.RelativeTimeFormatUnit, options?: { locales?: string | string[] } & Omit ): string ``` *Note: `formatRelativeTime` runs locally using `Intl.RelativeTimeFormat` and does not require an API key. It falls back to the instance's rendering locales when `locales` is omitted. For formatting without a `GT` instance, see the standalone [`formatRelativeTime`](/docs/platform/core/reference/utility-functions/formatting/format-relative-time).* ## How it works [#how-it-works] * **Locale resolution.** When `locales` is omitted, the method falls back to the instance's rendering locales. * **Intl-backed.** Formatting is delegated to the browser's native [`Intl.RelativeTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat). * **Defaults.** `numeric` defaults to `'auto'` (so `-1 day` becomes "yesterday" rather than "1 day ago") and `style` defaults to `'long'`. ## Parameters [#parameters] | Parameter | Description | Type | Optional | Default | | --------------------- | ----------------------------------------------------------------- | ----------------------------- | -------- | ------- | | [`value`](#value) | The relative time value (negative for past, positive for future). | `number` | No | — | | [`unit`](#unit) | The unit of time. | `Intl.RelativeTimeFormatUnit` | No | — | | [`options`](#options) | Formatting configuration. | `object` | Yes | — | ### `value` [#value] **Type** `number` · **Required** The relative time value. Negative values are in the past; positive values are in the future. ### `unit` [#unit] **Type** `Intl.RelativeTimeFormatUnit` · **Required** The unit of time: `'second'`, `'minute'`, `'hour'`, `'day'`, `'week'`, `'month'`, or `'year'`. ### `options` [#options] **Type** `{ locales?: string | string[] } & Omit` · **Optional** Formatting configuration: | Name | Description | Type | Optional | Default | | --------------- | --------------------------------------------------------------------------- | ------------------------------- | -------- | ---------------- | | `locales` | Locales for formatting. Falls back to the instance's rendering locales. | `string \| string[]` | Yes | instance locales | | `numeric` | Whether to always use numeric output. | `'always' \| 'auto'` | Yes | `'auto'` | | `style` | The length of the output. | `'long' \| 'short' \| 'narrow'` | Yes | `'long'` | | `localeMatcher` | The locale matching algorithm to use. | `'best fit' \| 'lookup'` | Yes | `'best fit'` | ## Returns [#returns] **Type** `string` The formatted relative time string. ## Examples [#examples] ```typescript import { GT } from 'generaltranslation'; const gt = new GT(); // Past time gt.formatRelativeTime(-2, 'hour', { locales: 'en-US' }); // Returns: "2 hours ago" // Future time gt.formatRelativeTime(3, 'day', { locales: 'fr-FR' }); // Returns: "dans 3 jours" // With numeric: 'auto' (default) gt.formatRelativeTime(-1, 'day', { locales: 'en-US' }); // Returns: "yesterday" ``` ## Notes [#notes] * Defaults to `numeric: 'auto'` and `style: 'long'`. * Uses `Intl.RelativeTimeFormat` under the hood. * To automatically select the unit from a `Date`, use [`formatRelativeTimeFromDate`](/docs/platform/core/reference/gt-class-methods/formatting/format-relative-time-from-date).