# generaltranslation: General Translation Core SDK: formatRelativeTimeFromDate URL: https://generaltranslation.com/en-US/docs/core/class/methods/formatting/format-relative-time-from-date.mdx --- title: formatRelativeTimeFromDate description: API reference for the formatRelativeTimeFromDate method to format relative time from a Date --- ## Overview The `formatRelativeTimeFromDate` method formats a relative time string from a `Date`, automatically selecting the most appropriate unit. ```typescript const gt = new GT(); const pastDate = new Date(Date.now() - 7200000); // 2 hours ago const formatted = gt.formatRelativeTimeFromDate(pastDate, { locales: 'en-US' }); // 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[]` | Locales for formatting. Falls back to the instance's rendering locales | | `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 ```typescript copy 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 * Automatically selects the best unit based on the time difference * Defaults to `numeric: 'auto'` and `style: 'long'` * If `baseDate` is not provided, defaults to `new Date()` ## Next steps * See [`formatRelativeTime`](/docs/core/class/methods/formatting/format-relative-time) for explicit value + unit formatting * See the standalone [`formatRelativeTimeFromDate`](/docs/core/functions/formatting/format-relative-time-from-date) function for usage without a GT instance