# generaltranslation: General Translation Core SDK: formatDateTime URL: https://generaltranslation.com/ja/docs/core/class/methods/formatting/format-date-time.mdx --- title: formatDateTime description: ロケールの規則に従って日付と時刻を整形する `formatDateTime` メソッドの API リファレンス --- ## 概要 `formatDateTime` メソッドは、Internationalization API を使用して、ロケール固有の規則に従って日付と時刻をフォーマットします。 対象のロケールに応じて、日付形式、時刻形式、カレンダー、タイムゾーンを自動的に処理します。 ```typescript const gt = new GT({ targetLocale: 'de-DE' }); const formatted = gt.formatDateTime(new Date(), { dateStyle: 'medium', timeStyle: 'short' }); // 戻り値: "25.09.2025, 18:06" (ドイツ語の日時フォーマット) ``` ## リファレンス ### パラメータ | Name | Type | Description | | ---------- | ----------------------- | ----------------- | | `date` | `Date` | 書式設定する対象の日付オブジェクト | | `options?` | `DateTimeFormatOptions` | 省略可能な書式設定オプション | ### DateTimeFormatOptions 追加のロケール指定を加えて `Intl.DateTimeFormatOptions` を拡張したものです。 | Name | Type | Description | | ------------------------- | --------------------------------------------------------------------------------------- | ----------------------------------------- | | `locales?` | `string \| string[]` | 書式設定に使用するロケールを上書きします (デフォルトはインスタンスのロケール) | | `localeMatcher?` | `'lookup' \| 'best fit'` | ロケール照合アルゴリズム (デフォルト: `'best fit'`) | | `dateStyle?` | `'full' \| 'long' \| 'medium' \| 'short'` | 日付全体の書式スタイル | | `timeStyle?` | `'full' \| 'long' \| 'medium' \| 'short'` | 時刻全体の書式スタイル | | `weekday?` | `'long' \| 'short' \| 'narrow'` | 曜日の表記 | | `era?` | `'long' \| 'short' \| 'narrow'` | 元号の表記 | | `year?` | `'numeric' \| '2-digit'` | 年の表記 | | `month?` | `'numeric' \| '2-digit' \| 'long' \| 'short' \| 'narrow'` | 月の表記 | | `day?` | `'numeric' \| '2-digit'` | 日の表記 | | `dayPeriod?` | `'narrow' \| 'short' \| 'long'` | 時間帯の書式 (午前、午後など) | | `hour?` | `'numeric' \| '2-digit'` | 時の表記 | | `minute?` | `'numeric' \| '2-digit'` | 分の表記 | | `second?` | `'numeric' \| '2-digit'` | 秒の表記 | | `fractionalSecondDigits?` | `1 \| 2 \| 3` | 秒の小数部の桁数 | | `timeZoneName?` | `'long' \| 'short' \| 'longOffset' \| 'shortOffset' \| 'longGeneric' \| 'shortGeneric'` | タイムゾーン名の形式 | | `timeZone?` | `string` | IANA タイムゾーン識別子 | | `hour12?` | `boolean` | 12 時間制を使用するかどうか | | `hourCycle?` | `'h11' \| 'h12' \| 'h23' \| 'h24'` | 時間サイクルの優先設定 | | `calendar?` | `string` | 使用する暦法 | | `numberingSystem?` | `string` | 数字の表記体系 | | `formatMatcher?` | `'basic' \| 'best fit'` | 書式照合アルゴリズム (デフォルト: `'best fit'`) | ### 戻り値 `string` - ロケールの規則に従って書式化された日付と時刻。 *** ## 例 ### 基本的な日付と時刻のフォーマット ```typescript copy 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 copy const date = new Date('2024-03-14T14:30:45Z'); // 完全な日付スタイル console.log(gt.formatDateTime(date, { dateStyle: 'full' })); // 出力: "Thursday, March 14, 2024" // 長い日付と短い時刻 console.log(gt.formatDateTime(date, { dateStyle: 'long', timeStyle: 'short' })); // 出力: "March 14, 2024 at 7:30 AM" // カスタム日付コンポーネント console.log(gt.formatDateTime(date, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' })); // 出力: "Thursday, March 14, 2024" ``` ### タイムゾーンと時刻表記 ```typescript copy 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" ``` *** ## 注記 * 日付の書式は、ロケール固有の規則に自動的に従います * このメソッドは、最適なパフォーマンスと精度のために、ブラウザネイティブの `Intl.DateTimeFormat` を使用します * タイムゾーンが指定されている場合は、正しく処理されます ## 関連メソッド * その他のオプションについては、[`Intl.DateTimeFormat` のドキュメント](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat)を参照してください * 日付補間を含むメッセージのフォーマットについては、[`formatMessage`](/docs/core/class/methods/formatting/format-message)を参照してください * GT インスタンスを使わずに使用する場合は、スタンドアロンの [`formatDateTime`](/docs/core/functions/formatting/format-date-time)を参照してください * ロケール固有のカレンダー情報については、[`getLocaleProperties`](/docs/core/class/methods/locales/get-locale-properties)を参照してください ## 次のステップ