# generaltranslation: General Translation Core SDK: formatRelativeTimeFromDate URL: https://generaltranslation.com/en-US/docs/core/functions/formatting/format-relative-time-from-date.mdx --- title: formatRelativeTimeFromDate description: Standalone function to format relative time from a Date, automatically selecting the best unit --- ## Overview The standalone `formatRelativeTimeFromDate` function formats a relative time string from a `Date`, automatically selecting the most appropriate unit (seconds, minutes, hours, days, weeks, months, or years). ```typescript import { formatRelativeTimeFromDate } from 'generaltranslation'; const pastDate = new Date(Date.now() - 7200000); // 2 hours ago const formatted = formatRelativeTimeFromDate(pastDate, { locales: 'en-US', baseDate: new Date() }); // Returns: "2 hours ago" ``` ## Reference ### Parameters | Name | Type | Description | |------|------|-------------| | `date` | `Date` | The date to format relative to `baseDate` | | `options` | `object` | Formatting configuration | ### Options | Name | Type | Description | |------|------|-------------| | `locales` | `string \| string[]` | **Required.** Locales for formatting | | `baseDate?` | `Date` | The base date for comparison. Defaults to `new Date()` | | `numeric?` | `'always' \| 'auto'` | Whether to always use numeric output. Defaults to `'auto'` | | `style?` | `'long' \| 'short' \| 'narrow'` | The length of the output. Defaults to `'long'` | | `localeMatcher?` | `'best fit' \| 'lookup'` | The locale matching algorithm to use | ### Returns `string` - The formatted relative time string (e.g., "2 hours ago", "in 3 days"). --- ## Example ### Basic usage ```typescript copy import { formatRelativeTimeFromDate } from 'generaltranslation'; const now = new Date(); // 2 hours ago const pastDate = new Date(now.getTime() - 7200000); console.log(formatRelativeTimeFromDate(pastDate, { locales: 'en-US', baseDate: now })); // Output: "2 hours ago" // 3 days in the future const futureDate = new Date(now.getTime() + 259200000); console.log(formatRelativeTimeFromDate(futureDate, { locales: 'en-US', baseDate: now })); // Output: "in 3 days" ``` ### Multiple locales ```typescript copy import { formatRelativeTimeFromDate } from 'generaltranslation'; const pastDate = new Date(Date.now() - 86400000); // ~1 day ago const now = new Date(); const locales = ['en-US', 'fr-FR', 'ja-JP', 'de-DE']; locales.forEach(locale => { console.log(`${locale}: ${formatRelativeTimeFromDate(pastDate, { locales: locale, baseDate: now })}`); }); // Output: // en-US: yesterday // fr-FR: hier // ja-JP: 昨日 // de-DE: gestern ``` --- ## Notes * Automatically selects the best unit based on the difference between `date` and `baseDate` * Defaults to `numeric: 'auto'` and `style: 'long'` * If `baseDate` is not provided, defaults to `new Date()` — be aware this can cause hydration mismatches in server-rendered apps * Uses [`Intl.RelativeTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat) under the hood ## Next steps * See [`formatRelativeTime`](/docs/core/functions/formatting/format-relative-time) for explicit value + unit formatting * See [`formatDateTime`](/docs/core/functions/formatting/format-date-time) for standalone date/time formatting * See GT class [`formatRelativeTimeFromDate`](/docs/core/class/methods/formatting/format-relative-time-from-date) for instance-based usage