# General Translation Platform: formatRelativeTimeFromDate URL: https://generaltranslation.com/ja/docs/platform/core/reference/gt-class-methods/formatting/format-relative-time-from-date.mdx --- title: formatRelativeTimeFromDate description: 現在時刻または別の日付を基準に、日付から相対時間をフォーマットします。formatRelativeTimeFromDate の API リファレンス。 --- [GT](/docs/platform/core/reference/gt-class/constructor) インスタンスで、`Date` から相対時間の文字列をフォーマットし、最も適切な単位を自動的に選択します。General Translation はその日付を基準日 (デフォルトでは現在時刻) と比較し、「2 時間前」や「3 日後」のような表現を生成します。 ## 概要 [#overview] 対象の `Date` と必要に応じてオプションオブジェクトを指定して、[`GT`](/docs/platform/core/reference/gt-class/constructor) インスタンスで `formatRelativeTimeFromDate` を呼び出します。時間差に最も適した単位を選択し、整形された文字列を返します。 ```typescript const gt = new GT(); const pastDate = new Date(Date.now() - 7200000); // 2時間前 const formatted = gt.formatRelativeTimeFromDate(pastDate, { locales: 'en-US', }); // "2時間前" ``` シグネチャ: ```typescript formatRelativeTimeFromDate( date: Date, options?: { locales?: string | string[]; baseDate?: Date; } & Omit ): string ``` *注: `formatRelativeTimeFromDate` は `Intl.RelativeTimeFormat` を使ってローカルで実行されるため、APIキーは不要です。`locales` を省略した場合は、インスタンスのレンダリングに使用されるロケールにフォールバックします。`GT` インスタンスを使わずに書式設定する場合は、スタンドアロンの [`formatRelativeTimeFromDate`](/docs/platform/core/reference/utility-functions/formatting/format-relative-time-from-date) を参照してください。* ## 動作の仕組み [#how-it-works] * **単位 の自動選択。** このメソッドは `date` と `baseDate` の差を計算し、最適な 単位 (秒、分、時間、日など) を選択します。 * **基準日。** 比較は `baseDate` を基準に行われ、デフォルトでは `new Date()` (現在時刻) が使われます。 * **ロケールの解決。** `locales` を省略した場合、このメソッドはインスタンスのレンダリングロケールにフォールバックします。 * **デフォルト値。** `numeric` のデフォルトは `'auto'`、`style` のデフォルトは `'long'` です。 ## パラメータ [#parameters] | パラメータ | 説明 | Type | 任意 | デフォルト | | --------------------- | --------------------------- | -------- | --- | ----- | | [`date`](#date) | `baseDate` を基準に相対形式で表示する日付。 | `Date` | いいえ | — | | [`options`](#options) | フォーマットの設定。 | `object` | はい | — | ### `date` [#date] **型** `Date` · **必須** `baseDate` を基準に相対形式で書式設定する日付です。 ### `options` [#options] **型** `{ locales?: string | string[]; baseDate?: Date } & Omit` · **任意** 書式設定オプション: | 名前 | 説明 | Type | 任意 | デフォルト | | --------------- | ------------------------------------------------------- | ------------------------------- | -- | --------------- | | `locales` | 書式設定に使用するロケール。指定しない場合は、インスタンスのレンダリング `locales` が使用されます。 | `string \| string[]` | はい | インスタンスの locales | | `baseDate` | 比較に使用する基準日。 | `Date` | はい | `new Date()` | | `numeric` | 常に数値形式の出力を使用するかどうか。 | `'always' \| 'auto'` | はい | `'auto'` | | `style` | 出力の長さ。 | `'long' \| 'short' \| 'narrow'` | はい | `'long'` | | `localeMatcher` | 使用するロケール照合アルゴリズム。 | `'best fit' \| 'lookup'` | はい | `'best fit'` | ## 戻り値 [#returns] **型** `string` 「2時間前」や「3日後」のような、書式設定済みの相対時間文字列。 ## 例 [#examples] ```typescript import { GT } from 'generaltranslation'; const gt = new GT(); const now = new Date(); // 自動的に "hours" を選択 const twoHoursAgo = new Date(now.getTime() - 7200000); gt.formatRelativeTimeFromDate(twoHoursAgo, { locales: 'en-US', baseDate: now }); // 戻り値: "2 hours ago" // 自動的に "days" を選択 const threeDaysLater = new Date(now.getTime() + 259200000); gt.formatRelativeTimeFromDate(threeDaysLater, { locales: 'fr-FR', baseDate: now }); // 戻り値: "dans 3 jours" ``` ## メモ [#notes] * 時間差に基づいて、最適な単位を自動的に選択します。 * デフォルトでは `numeric: 'auto'` と `style: 'long'` が使用されます。 * `baseDate` が指定されていない場合は、`new Date()` がデフォルトで使用されます。 * 値と単位を明示的に指定して書式設定するには、[`formatRelativeTime`](/docs/platform/core/reference/gt-class-methods/formatting/format-relative-time) を使用します。