# General Translation Platform: formatDateTime URL: https://generaltranslation.com/ja/docs/platform/core/reference/utility-functions/formatting/format-date-time.mdx --- title: formatDateTime description: GT インスタンスなしで日付と時刻をフォーマットします。formatDateTime の API リファレンス。 --- [`formatDateTime`](/docs/platform/core/reference/gt-class-methods/formatting/format-date-time) は、General Translation のコアライブラリに含まれるスタンドアロンのユーティリティ関数で、ロケール固有の規則に従って日付と時刻をフォーマットします。`Date` を、ロケールに応じた文字列として返します。 ## 概要 [#overview] `generaltranslation` から `formatDateTime` を直接インポートし、`Date` とオプションオブジェクトを渡して呼び出します。API キーや [GT](/docs/platform/core/reference/gt-class/constructor) インスタンスは必要ありません。インスタンスのロケールを継承してフォーマットするには、代わりに [`GT`](/docs/platform/core/reference/gt-class/constructor) インスタンスの [`formatDateTime`](/docs/platform/core/reference/gt-class-methods/formatting/format-date-time) メソッドを使用してください。 ```typescript import { formatDateTime } from 'generaltranslation'; const formatted = formatDateTime(new Date(), { locales: 'de-DE', dateStyle: 'medium', timeStyle: 'short', }); // ロケールに応じてフォーマットされた文字列を返します(例:"26.09.2025, 17:33") ``` シグネチャ: ```typescript formatDateTime( date: Date, options?: { locales?: string | string[] } & Intl.DateTimeFormatOptions ): string ``` ## 仕組み [#how-it-works] * **基盤となる API。** GT class method と同じ [`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) を使用しているため、標準の `Intl.DateTimeFormat` オプションをすべて利用できます。 * **ロケールの解決。** `locales` が配列の場合、ロケールは順に試されます。`locales` を省略した場合は、ライブラリのデフォルトロケールである `en` が使用されます。 * **タイムゾーン。** `timeZone` オプションが指定されている場合、出力はそれに従います。指定されていない場合は、ランタイムのローカルタイムゾーンが使用されます。ロケールによって、デフォルトの日付/時刻形式や、12 時間表記と 24 時間表記のどちらを使うかが異なります。 * **キャッシュ。** 同じロケールとオプションの組み合わせが繰り返し使われる場合のパフォーマンス向上のため、結果は内部的にキャッシュされます。 ## パラメータ [#parameters] | パラメータ | 説明 | 型 | 任意 | デフォルト | | --------------------- | -------------------------------------------------- | --------------------------------------------------------------- | --- | ----- | | [`date`](#date) | フォーマットする日付オブジェクト。 | `Date` | いいえ | — | | [`options`](#options) | 対象ロケールと `Intl.DateTimeFormat` の各種オプションを含むフォーマット設定。 | `{ locales?: string \| string[] } & Intl.DateTimeFormatOptions` | はい | `{}` | ### `date` [#date] **型** `Date` · **必須** フォーマット対象の `Date` オブジェクト。 ### `options` [#options] **型** `{ locales?: string | string[] } & Intl.DateTimeFormatOptions` · **任意** · **デフォルト** `{}` 書式設定のオプションです。`locales` に加えて、[`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) の任意のオプションを指定できます。主なフィールドは次のとおりです。 | プロパティ | 説明 | 型 | 任意 | デフォルト | | ----------- | -------------------------------------- | --------------------------------------------------------- | -- | ----- | | `locales` | 書式設定に使用するロケールです。配列を渡した場合は、先頭から順に試されます。 | `string \| string[]` | はい | `en` | | `dateStyle` | 日付全体の書式スタイルです。 | `'full' \| 'long' \| 'medium' \| 'short'` | はい | — | | `timeStyle` | 時刻全体の書式スタイルです。 | `'full' \| 'long' \| 'medium' \| 'short'` | はい | — | | `weekday` | 曜日の表現形式です。 | `'long' \| 'short' \| 'narrow'` | はい | — | | `year` | 年の表現形式です。 | `'numeric' \| '2-digit'` | はい | — | | `month` | 月の表現形式です。 | `'numeric' \| '2-digit' \| 'long' \| 'short' \| 'narrow'` | はい | — | | `day` | 日の表現形式です。 | `'numeric' \| '2-digit'` | はい | — | | `hour` | 時の表現形式です。 | `'numeric' \| '2-digit'` | はい | — | | `minute` | 分の表現形式です。 | `'numeric' \| '2-digit'` | はい | — | | `second` | 秒の表現形式です。 | `'numeric' \| '2-digit'` | はい | — | | `timeZone` | IANA タイムゾーン識別子です。 | `string` | はい | — | | `hour12` | 12時間制を使用するかどうかです。 | `boolean` | はい | — | ## 戻り値 [#returns] **型** `string` ロケールの規則に従って書式設定された日時。 ## 例 [#examples] ```typescript import { formatDateTime } from 'generaltranslation'; const date = new Date('2024-03-14T14:30:45Z'); // 明示的なロケールを使用した基本的なフォーマット console.log(formatDateTime(date, { locales: 'en-US', timeZone: 'UTC' })); // 出力: "3/14/2024" // ドイツ語のフォーマット console.log(formatDateTime(date, { locales: 'de-DE', timeZone: 'UTC' })); // 出力: "14.3.2024" // 複数のロケールのフォールバック console.log(formatDateTime(date, { locales: ['ja-JP', 'en-US'], timeZone: 'UTC' })); // 出力: "2024/3/14"(日本語フォーマット) ``` ```typescript // 日付と時刻のスタイル const date = new Date('2024-03-14T14:30:45Z'); // 完全な日付スタイル console.log(formatDateTime(date, { locales: 'en-US', dateStyle: 'full', timeZone: 'UTC', })); // 出力: "Thursday, March 14, 2024" // 長い日付と短い時刻 console.log(formatDateTime(date, { locales: 'fr-FR', dateStyle: 'long', timeStyle: 'short', timeZone: 'UTC', })); // 出力: "14 mars 2024 à 14:30" ``` ```typescript // タイムゾーンの処理 const date = new Date('2024-03-14T14:30:45Z'); const timeZones = ['America/New_York', 'Europe/London', 'Asia/Tokyo']; timeZones.forEach((timeZone) => { const formatted = formatDateTime(date, { locales: 'en-US', timeZone, dateStyle: 'medium', timeStyle: 'medium', }); console.log(`${timeZone}: ${formatted}`); }); // 出力は夏時間によって異なります ``` ## メモ [#notes] * GT class method と同じ `Intl.DateTimeFormat` を内部で使用しています。 * 同じロケール/オプションの組み合わせが繰り返される場合のパフォーマンス向上のため、結果は内部的にキャッシュされます。 * 標準の `Intl.DateTimeFormat` オプションはすべてサポートされています。 * タイムゾーンを指定した場合は、正しく処理されます。固定の `timeZone` を指定しない場合の出力は、Runtime 環境に依存します。