Intl.DateTimeFormat オブジェクトは、言語に応じた日付と時刻のフォーマットを可能にするオブジェクトのためのコンストラクタです。
構文
new Intl.DateTimeFormat([locales[, options]])
Intl.DateTimeFormat.call(this[, locales[, options]])
引数
locales-
任意。BCP47 言語タグの文字列、または、そのような文字列の配列。
locales引数の一般的な形式と解釈は、Intl page をご覧ください。次の Unicode 拡張キーが許可されています:nu- 番号方式。使用できる値は以下のとおりです:
"arab","arabext","bali","beng","deva","fullwide","gujr","guru","hanidec","khmr","knda","laoo","latn","limb","mlym","mong","mymr","orya","tamldec","telu","thai","tibt" ca- カレンダー。使用できる値は以下のとおりです。:
"buddhist","chinese","coptic","ethioaa","ethiopic","gregory","hebrew","indian","islamic","islamicc","iso8601","japanese","persian","roc"
options-
任意。以下のプロパティの一部またはすべてを持つオブジェクト:
localeMatcher- 使用するロケールマッチングアルゴリズム。可能な値は
"lookup"と"best fit"です。デフォルトは"best fit"です。このオプションについての詳細は、Intl page をご覧ください。 timeZone- 使用するタイムゾーン。実装が認識しなければならない唯一の値は
"UTC"です。デフォルトは、実行時のデフォルトのタイムゾーンです。実装は、また、"Asia/Shanghai","Asia/Kolkata","America/New_York"のような IANA time zone database のタイムゾーン名を認識できます。 hour12- 12 時間表示を使用するかどうか (反対は 24 時間表示)。可能な値は
trueとfalseです。デフォルトはロケール依存です。 formatMatcher- 使用するフォーマットマッチングアルゴリズム。可能な値は
"basic"と"best fit"です。デフォルトは"best fit"です。このプロパティの使用方法については、以下の項を参照してください。
次のプロパティは、日付·時刻のコンポーネントやそれらの所望の表現をフォーマットされた出力で使用するように記述します。実装は、少なくとも以下のサブセットをサポートするために必要です:
weekday,year,month,day,hour,minute,secondweekday,year,month,dayyear,month,dayyear,monthmonth,dayhour,minute,secondhour,minute
実装は、他のサブセットもサポートし、要求は、ベストマッチを見つけるために、すべての利用可能なサブセットの表現の組み合わせに対してネゴシエートされます。二つのアルゴリズムがこのネゴシエートに対して利用可能で、
formatMatcherプロパティによって選択されます: fully specified"basic"algorithm と"best fit"アルゴリズムに依存した実装。weekday- 平日の表現。可能な値は、
"narrow","short","long"です。 era- 時代の表現。可能な値は
"narrow","short","long"です。 year- 今年の表現。可能な値は
"numeric","2-digit"です。 month- 月の表現。可能な値は
"numeric","2-digit","narrow","short","long"です。 day- 日の表現。可能な値は
"numeric","2-digit"です。 hour- 時間の表現。可能な値は
"numeric","2-digit"です。 minute- 分の表現。可能な値は
"numeric","2-digit"です。 second- 秒の表現。可能な値は
"numeric","2-digit"です。 timeZoneName- タイムゾーン名の表現。可能な値は
"short","long"です。
各日付時間コンポーネントプロパティのデフォルト値は
undefinedですが、すべてのコンポーネントプロパティがundefinedの場合、year,month,dayは"numeric"であると仮定されています。
説明
プロパティ
Intl.DateTimeFormat.prototype- すべてのオブジェクトにプロパティを追加できます。
メソッド
Intl.DateTimeFormat.supportedLocalesOf()- 実行時のデフォルトロケールにフォールバックすることなく、サポートされて提供されるロケールのものを含む配列を返します。
DateTimeFormat インスタンス
プロパティ
DateTimeFormat インスタンスはプロトタイプから以下ののプロパティを継承します:
Intl.DateTimeFormat.prototype.constructor- A reference to
Intl.DateTimeFormat. Intl.DateTimeFormat.prototype.format- Getter; returns a function that formats a date according to the locale and formatting options of this
DateTimeFormatobject.
メソッド
DateTimeFormat インスタンスはプロトタイプから次のメソッドを継承します:
Intl.DateTimeFormat.prototype.resolvedOptions()- Returns a new object with properties reflecting the locale and formatting options computed during initialization of the object.
例
DateTimeFormat を使う
ロケールを指定しない基本的な使用では、DateTimeFormat はデフォルトロケールとデフォルトオプションを使用します。
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); // toLocaleString without arguments depends on the implementation, // the default locale, and the default time zone console.log(new Intl.DateTimeFormat().format(date)); // → "12/19/2012" if run in en-US locale with time zone America/Los_Angeles
locales を使う
この例では、ローカライズされた日付と時刻の形式のバリエーションのいくつかを示しています。アプリケーションのユーザインターフェイスで使用される言語のフォーマットを取得するために、locales 引数を使用して、その言語 (おそらくいくつかのフォールバック言語) を指定してください:
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// formats below assume the local time zone of the locale;
// America/Los_Angeles for the US
// US English uses month-day-year order
console.log(new Intl.DateTimeFormat('en-US').format(date));
// → "12/19/2012"
// British English uses day-month-year order
console.log(new Intl.DateTimeFormat('en-GB').format(date));
// → "20/12/2012"
// Korean uses year-month-day order
console.log(new Intl.DateTimeFormat('ko-KR').format(date));
// → "2012. 12. 20."
// Arabic in most Arabic speaking countries uses real Arabic digits
console.log(new Intl.DateTimeFormat('ar-EG').format(date));
// → "٢٠/١٢/٢٠١٢"
// for Japanese, applications may want to use the Japanese calendar,
// where 2012 was the year 24 of the Heisei era
console.log(new Intl.DateTimeFormat('ja-JP-u-ca-japanese').format(date));
// → "24/12/20"
// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
console.log(new Intl.DateTimeFormat(['ban', 'id']).format(date));
// → "20/12/2012"
options を使う
日付と時刻のフォーマットは options 引数を使用してカスタマイズできます:
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// request a weekday along with a long date
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
console.log(new Intl.DateTimeFormat('de-DE', options).format(date));
// → "Donnerstag, 20. Dezember 2012"
// an application may want to use UTC and make that visible
options.timeZone = 'UTC';
options.timeZoneName = 'short';
console.log(new Intl.DateTimeFormat('en-US', options).format(date));
// → "Thursday, December 20, 2012, GMT"
// sometimes you want to be more precise
options = {
hour: 'numeric', minute: 'numeric', second: 'numeric',
timeZoneName: 'short'
};
console.log(new Intl.DateTimeFormat('en-AU', options).format(date));
// → "2:00:00 pm AEDT"
// sometimes even the US needs 24-hour time
options = {
year: 'numeric', month: 'numeric', day: 'numeric',
hour: 'numeric', minute: 'numeric', second: 'numeric',
hour12: false
};
console.log(date.toLocaleString('en-US', options));
// → "12/19/2012, 19:00:00"
仕様
| 仕様 | 状況 | コメント |
|---|---|---|
| ECMAScript Internationalization API 1.0 (ECMA-402) The definition of 'Intl.DateTimeFormat' in that specification. |
Standard | 初期定義。 |
ブラウザ実装状況
| 機能 | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| 基本サポート | 24 | 29 (29) | 11 | 15 | 未サポート |
| 機能 | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| 基本サポート | 未サポート | 26 | 未サポート バグ 864843 |
未サポート | 未サポート | 未サポート |