GT ClassMethodsFormatting
formatDateTime
localeの慣習に従って日付と時刻をフォーマットするformatDateTimeメソッドのAPIリファレンス
概要
formatDateTime メソッドは Internationalization API を用いて、locale に固有の慣習に従って日付と時刻をフォーマットします。
対象の locale に基づき、日付形式、時刻形式、暦、タイムゾーンを自動的に処理します。
const gt = new GT({ targetLocale: 'de-DE' });
const formatted = gt.formatDateTime(new Date(), {
  dateStyle: 'medium',
  timeStyle: 'short'
});
// 戻り値: "25.09.2025, 18:06"(ドイツ語の日時書式)リファレンス
パラメータ
| 名称 | 型 | 説明 | 
|---|---|---|
| date | Date | フォーマット対象の日付オブジェクト | 
| options? | DateTimeFormatOptions | 任意のフォーマット設定 | 
DateTimeFormatOptions
Intl.DateTimeFormatOptions に、追加のロケール指定を加えて拡張します:
| Name | Type | Description | 
|---|---|---|
| locales? | string | string[] | フォーマット時に使用する対応ロケールを上書きします(デフォルトはインスタンスの locales) | 
| 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 - locale の規約に従ってフォーマットされた日付と時刻。
例
日付と時刻の基本的なフォーマット
import { GT } from 'generaltranslation';
const gt = new GT({ targetLocale: 'en-US' });
const date = new Date('2024-03-14T14:30:45Z');
// 基本的な日付フォーマット(デフォルトのoptionsを使用)
console.log(gt.formatDateTime(date));
// 出力: "3/14/2024"
// ドイツ語ロケールでのフォーマット
console.log(gt.formatDateTime(date, { locales: 'de-DE' }));
// 出力: "14.3.2024"
// 日本語ロケールでのフォーマット
console.log(gt.formatDateTime(date, { locales: 'ja-JP' }));
// 出力: "2024/3/14"日付・時刻のスタイル
const date = new Date('2024-03-14T14:30:45Z');
// 日付スタイル(完全)
console.log(gt.formatDateTime(date, { dateStyle: 'full' }));
// 出力: "木曜日、2024年3月14日"
// 長い日付 + 短い時刻
console.log(gt.formatDateTime(date, {
  dateStyle: 'long',
  timeStyle: 'short'
}));
// 出力: "2024年3月14日 7:30"
// 日付コンポーネントのカスタム指定
console.log(gt.formatDateTime(date, {
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric'
}));
// 出力: "木曜日、2024年3月14日"タイムゾーンと時間表記
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"注意事項
- 日付のフォーマットは、locale ごとの規約に自動的に従います
- このメソッドは、最適なパフォーマンスと精度のために、ブラウザのネイティブ機能である Intl.DateTimeFormatを使用します
- タイムゾーンは、指定された場合に正しく処理されます
関連メソッド
- さらに多くの options については Intl.DateTimeFormatのドキュメントを参照してください
- 相対日時のフォーマット(「2 days ago」など)については formatRelativeTimeを参照してください
- 日付の差し込みを含むメッセージのフォーマットについては formatMessageを参照してください
- GT インスタンスなしで使用する場合はスタンドアロンの formatDateTimeを参照してください
- ロケール固有のカレンダー情報については getLocalePropertiesを参照してください
次のステップ
- 日付・時刻のフォーマット戦略の詳細は Formatting Guide を参照してください
- locale に応じた日付処理については Internationalization Guide を参照してください
このガイドはどうでしたか?

