# General Translation Platform: formatRelativeTimeFromDate URL: https://generaltranslation.com/en-US/docs/platform/core/reference/gt-class-methods/formatting/format-relative-time-from-date.mdx --- title: formatRelativeTimeFromDate description: Format relative time from a date compared with the current time or another date. API reference for formatRelativeTimeFromDate. --- Formats a relative time string from a `Date`, automatically selecting the most appropriate unit, on a [GT](/docs/platform/core/reference/gt-class/constructor) instance. General Translation compares the date against a base date (the current time by default) and produces phrases such as "2 hours ago" or "in 3 days". ## Overview [#overview] Call `formatRelativeTimeFromDate` on a [`GT`](/docs/platform/core/reference/gt-class/constructor) instance with the target `Date` and an optional options object. It returns the formatted string, choosing the unit that best fits the time difference. ```typescript const gt = new GT(); const pastDate = new Date(Date.now() - 7200000); // 2 hours ago const formatted = gt.formatRelativeTimeFromDate(pastDate, { locales: 'en-US', }); // "2 hours ago" ``` Signature: ```typescript formatRelativeTimeFromDate( date: Date, options?: { locales?: string | string[]; baseDate?: Date; } & Omit ): string ``` *Note: `formatRelativeTimeFromDate` 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 [`formatRelativeTimeFromDate`](/docs/platform/core/reference/utility-functions/formatting/format-relative-time-from-date).* ## How it works [#how-it-works] - **Automatic unit selection.** The method computes the difference between `date` and `baseDate` and picks the best unit (seconds, minutes, hours, days, and so on). - **Base date.** Comparison is against `baseDate`, which defaults to `new Date()` (the current time). - **Locale resolution.** When `locales` is omitted, the method falls back to the instance's rendering locales. - **Defaults.** `numeric` defaults to `'auto'` and `style` defaults to `'long'`. ## Parameters [#parameters] | Parameter | Description | Type | Optional | Default | | --- | --- | --- | --- | --- | | [`date`](#date) | The date to format relative to `baseDate`. | `Date` | No | — | | [`options`](#options) | Formatting configuration. | `object` | Yes | — | ### `date` [#date] **Type** `Date` · **Required** The date to format relative to `baseDate`. ### `options` [#options] **Type** `{ locales?: string | string[]; baseDate?: Date } & 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 | | `baseDate` | The base date for comparison. | `Date` | Yes | `new Date()` | | `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, such as "2 hours ago" or "in 3 days". ## Examples [#examples] ```typescript import { GT } from 'generaltranslation'; const gt = new GT(); const now = new Date(); // Auto-selects "hours" const twoHoursAgo = new Date(now.getTime() - 7200000); gt.formatRelativeTimeFromDate(twoHoursAgo, { locales: 'en-US', baseDate: now }); // Returns: "2 hours ago" // Auto-selects "days" const threeDaysLater = new Date(now.getTime() + 259200000); gt.formatRelativeTimeFromDate(threeDaysLater, { locales: 'fr-FR', baseDate: now }); // Returns: "dans 3 jours" ``` ## Notes [#notes] - Automatically selects the best unit based on the time difference. - Defaults to `numeric: 'auto'` and `style: 'long'`. - If `baseDate` is not provided, it defaults to `new Date()`. - For explicit value + unit formatting, use [`formatRelativeTime`](/docs/platform/core/reference/gt-class-methods/formatting/format-relative-time).