# General Translation Platform: formatDateTime
URL: https://generaltranslation.com/ja/docs/platform/core/reference/gt-class-methods/formatting/format-date-time.mdx
Docs index: https://generaltranslation.com/llms.txt
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キーは必要ありません。デフォルトではインスタンスの対象ロケールが使用され、利用できない場合はソースロケール、次に `en` にフォールバックします。`locales` を渡すことで上書きできます。`GT` インスタンスを使わずに書式設定する場合は、スタンドアロンの [`formatDateTime`](/docs/platform/core/reference/utility-functions/formatting/format-date-time) を参照してください。*

## 仕組み [#how-it-works]

* **ロケールの解決。** デフォルトでは、このメソッドはインスタンスの対象ロケールで書式設定し、ソースロケール、次に `en` にフォールバックします。単一の呼び出しに対してのみ上書きするには、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]

**型** `{ locales?: string | string[] } & Intl.DateTimeFormatOptions` · **任意**

フォーマットの構成です。この表には、公開されている Core 型で提供される一般的なオプションと、それらに適用される Core のデフォルト値を示します (標準仕様および ランタイム 固有の補足情報については、[`Intl.DateTimeFormat` コンストラクターのオプション](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#options)を参照してください) 。

| Name                     | 説明                                     | Type                                                                                    | 任意  | Default                                |
| ------------------------ | -------------------------------------- | --------------------------------------------------------------------------------------- | --- | -------------------------------------- |
| `locales`                | フォーマットに使用するロケールを上書きします。                | `string \| string[]`                                                                    | Yes | `targetLocale` → `sourceLocale` → `en` |
| `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 | スタイルまたはコンポーネントが設定されていない場合は `'numeric'` |
| `month`                  | 月の表記。                                  | `'numeric' \| '2-digit' \| 'long' \| 'short' \| 'narrow'`                               | Yes | スタイルまたはコンポーネントが設定されていない場合は `'numeric'` |
| `day`                    | 日の表記。                                  | `'numeric' \| '2-digit'`                                                                | Yes | スタイルまたはコンポーネントが設定されていない場合は `'numeric'` |
| `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 タイムゾーン名またはサポートされている UTC オフセット識別子。 | `string`                                                                                | Yes | ランタイム のタイムゾーン                          |
| `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'`                           |

`dateStyle` と `timeStyle` は互いに組み合わせられますが、`year`、`month`、`hour` などの個別の日付時刻コンポーネントオプションとは組み合わせられません。`hour12` は `hourCycle` を上書きし、`dayPeriod` は 12 時間制のサイクルにのみ影響します。Core は `calendar: 'gregory'` と `numberingSystem: 'latn'` を設定します。それ以外は、基盤となる `Intl.DateTimeFormat` がどちらもロケールから選択します。

## 戻り値 [#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` を使用します。
* タイムゾーンが指定されている場合も、正しく処理されます。

## Sitemap

See the full [sitemap](https://generaltranslation.com/sitemap.md) for all pages.
