# General Translation Platform: formatDateTime URL: https://generaltranslation.com/ja/docs/platform/core/reference/gt-class-methods/formatting/format-date-time.mdx --- title: formatDateTime description: ロケールに応じて日付と時刻をフォーマットします。formatDateTime の API リファレンス。 --- [GT](/docs/platform/core/reference/gt-class/constructor) インスタンスで、ロケール固有の規則に従って日付と時刻をフォーマットします。General Translation は、組み込みの `Intl.DateTimeFormat` API を使用して、対象ロケールの日付形式、時刻形式、カレンダー、タイムゾーンを自動的に処理します。 ## 概要 [#overview] [`GT`](/docs/platform/core/reference/gt-class/constructor) インスタンスに対して、`Date` と省略可能なオプションオブジェクトを指定して `formatDateTime` を呼び出します。書式設定された日付と時刻を文字列として返します。 ```typescript const gt = new GT({ targetLocale: 'de-DE' }); const formatted = gt.formatDateTime(new Date(), { dateStyle: 'medium', timeStyle: 'short', }); // "25.09.2025, 18:06" (ドイツ語の日時フォーマット) ``` シグネチャ: ```typescript formatDateTime( date: Date, options?: { locales?: string | string[] } & Intl.DateTimeFormatOptions ): string ``` *注: `formatDateTime` は `Intl.DateTimeFormat` を使ってローカルで実行されるため、APIキーは必要ありません。デフォルトではインスタンスのレンダリングロケールが使用されますが、`locales` を渡すことで上書きできます。`GT` インスタンスを使わずに書式設定する場合は、スタンドアロンの [`formatDateTime`](/docs/platform/core/reference/utility-functions/formatting/format-date-time) を参照してください。* ## 仕組み [#how-it-works] * **ロケールの解決。** デフォルトでは、このメソッドはインスタンスのロケールで書式設定します。単一の呼び出しに対してのみ上書きするには、options に `locales` を渡します。 * **Intl ベース。** 書式設定はブラウザネイティブの [`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) に委ねられるため、標準の `Intl.DateTimeFormatOptions` はすべてサポートされます。 * **タイムゾーン。** `timeZone` が指定されている場合はタイムゾーンが正しく処理され、指定されていない場合はランタイムのローカルタイムゾーンが使用されます。 ## パラメータ [#parameters] | パラメータ | 説明 | 型 | 任意 | デフォルト | | --------------------- | ---------------------------------------------------------------- | -------- | --- | ----- | | [`date`](#date) | 書式設定する日付オブジェクト。 | `Date` | いいえ | — | | [`options`](#options) | `locales` による上書きを含む `Intl.DateTimeFormatOptions` を拡張した、書式設定用の構成。 | `object` | はい | — | ### `date` [#date] **型** `Date` · **必須** フォーマット対象の `Date` オブジェクト。 ### `options` [#options] **Type** `{ locales?: string | string[] } & Intl.DateTimeFormatOptions` · **任意** フォーマット設定です。`Intl.DateTimeFormatOptions` に追加の `locales` フィールドを加えて拡張します。 | Name | Description | Type | Optional | Default | | ------------------------ | ----------------------- | --------------------------------------------------------------------------------------- | -------- | --------------- | | `locales` | フォーマットに使用するロケールを上書きします。 | `string \| string[]` | Yes | インスタンスのロケール | | `localeMatcher` | ロケール照合アルゴリズム。 | `'lookup' \| 'best fit'` | Yes | `'best fit'` | | `dateStyle` | 日付全体のフォーマットスタイル。 | `'full' \| 'long' \| 'medium' \| 'short'` | Yes | — | | `timeStyle` | 時刻全体のフォーマットスタイル。 | `'full' \| 'long' \| 'medium' \| 'short'` | Yes | — | | `weekday` | 曜日の表記。 | `'long' \| 'short' \| 'narrow'` | Yes | — | | `era` | 紀元の表記。 | `'long' \| 'short' \| 'narrow'` | Yes | — | | `year` | 年の表記。 | `'numeric' \| '2-digit'` | Yes | — | | `month` | 月の表記。 | `'numeric' \| '2-digit' \| 'long' \| 'short' \| 'narrow'` | Yes | — | | `day` | 日の表記。 | `'numeric' \| '2-digit'` | Yes | — | | `dayPeriod` | 時間帯のフォーマット (午前、午後など) 。 | `'narrow' \| 'short' \| 'long'` | Yes | — | | `hour` | 時の表記。 | `'numeric' \| '2-digit'` | Yes | — | | `minute` | 分の表記。 | `'numeric' \| '2-digit'` | Yes | — | | `second` | 秒の表記。 | `'numeric' \| '2-digit'` | Yes | — | | `fractionalSecondDigits` | 秒の小数部の桁数。 | `1 \| 2 \| 3` | Yes | — | | `timeZoneName` | タイムゾーン名の形式。 | `'long' \| 'short' \| 'longOffset' \| 'shortOffset' \| 'longGeneric' \| 'shortGeneric'` | Yes | — | | `timeZone` | IANA タイムゾーン識別子。 | `string` | Yes | Runtime のタイムゾーン | | `hour12` | 12 時間制を使用するかどうか。 | `boolean` | Yes | — | | `hourCycle` | 時間サイクルの優先設定。 | `'h11' \| 'h12' \| 'h23' \| 'h24'` | Yes | — | | `calendar` | 使用するカレンダーシステム。 | `string` | Yes | `'gregory'` | | `numberingSystem` | 数字の表記体系。 | `string` | Yes | `'latn'` | | `formatMatcher` | フォーマット照合アルゴリズム。 | `'basic' \| 'best fit'` | Yes | `'best fit'` | ## 戻り値 [#returns] **型** `string` 対象ロケールの規則に従ってフォーマットされた日付と時刻。 ## 例 [#examples] *注: `timeZone` を明示していない例は、Runtime のローカルタイムゾーンで表示されます。以下の時刻の出力は、この日付時点での `America/Los_Angeles` (UTC−7) を前提としています。* ```typescript import { GT } from 'generaltranslation'; const gt = new GT({ targetLocale: 'en-US' }); const date = new Date('2024-03-14T14:30:45Z'); // 基本的な日付フォーマット(デフォルトオプションを使用) console.log(gt.formatDateTime(date)); // Output: "3/14/2024" // ドイツ語ロケールのフォーマット console.log(gt.formatDateTime(date, { locales: 'de-DE' })); // Output: "14.3.2024" // 日本語ロケールのフォーマット console.log(gt.formatDateTime(date, { locales: 'ja-JP' })); // Output: "2024/3/14" ``` ```typescript // 日付と時刻のスタイル const date = new Date('2024-03-14T14:30:45Z'); // 完全な日付スタイル console.log(gt.formatDateTime(date, { dateStyle: 'full' })); // Output: "Thursday, March 14, 2024" // 長い日付と短い時刻 console.log(gt.formatDateTime(date, { dateStyle: 'long', timeStyle: 'short', })); // Output: "March 14, 2024 at 7:30 AM" // カスタム日付コンポーネント console.log(gt.formatDateTime(date, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', })); // Output: "Thursday, March 14, 2024" ``` ```typescript // タイムゾーンと時刻形式 const date = new Date('2024-03-14T14:30:45Z'); // 12時間形式を強制 console.log(gt.formatDateTime(date, { hour: 'numeric', minute: '2-digit', hour12: true, })); // 出力: "7:30 AM" // 24時間形式を強制 console.log(gt.formatDateTime(date, { hour: 'numeric', minute: '2-digit', hour12: false, })); // 出力: "07:30" // 特定のタイムゾーン console.log(gt.formatDateTime(date, { timeZone: 'America/New_York', dateStyle: 'medium', timeStyle: 'short', })); // 出力: "Mar 14, 2024, 10:30 AM" ``` ## メモ [#notes] * 日付の書式は、ロケール固有の規則に自動的に従います。 * このメソッドでは、パフォーマンスと精度のためにブラウザネイティブの `Intl.DateTimeFormat` を使用します。 * タイムゾーンが指定されている場合も、正しく処理されます。