Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 682889 of Intl.DateTimeFormat

  • Revision slug: Web/JavaScript/Reference/Global_Objects/DateTimeFormat
  • Revision title: Intl.DateTimeFormat
  • Revision id: 682889
  • Created:
  • Creator: Mingun
  • Is current revision? No
  • Comment Standartized markup, added jsxref and Compat* templates, replaced `alert` to `console.log`, double to single quotes, fixed ids.

Revision Content

{{JSRef("Global_Objects", "DateTimeFormat", "Intl,Collator,NumberFormat")}}

Summary

The Intl.DateTimeFormat object is a constructor for objects that enable language sensitive date and time formatting.

Syntax

new Intl.DateTimeFormat([locales[, options]])
Intl.DateTimeFormat.call(this[, locales[, options]])

Parameters

locales

Optional. A string with a BCP 47 language tag, or an array of such strings. For the general form and interpretation of the locales argument, see the {{jsxref("Global_Objects/Intl", "Intl page", "#Locale_identification_and_negotiation", 1)}}. The following Unicode extension keys are allowed:

nu
Numbering system. Possible values include: "arab", "arabext", "bali", "beng", "deva", "fullwide", "gujr", "guru", "hanidec", "khmr", "knda", "laoo", "latn", "limb", "mlym", "mong", "mymr", "orya", "tamldec", "telu", "thai", "tibt".
ca
Calendar. Possible values include: "buddhist", "chinese", "coptic", "ethioaa", "ethiopic", "gregory", "hebrew", "indian", "islamic", "islamicc", "iso8601", "japanese", "persian", "roc".
options

Optional. An object with some or all of the following properties:

localeMatcher
The locale matching algorithm to use. Possible values are "lookup" and "best fit"; the default is "best fit". For information about this option, see the {{jsxref("Global_Objects/Intl", "Intl page", "#Locale_negotiation", 1)}}.
timeZone
The time zone to use. The only value implementations must recognize is "UTC"; the default is the runtime's default time zone. Implementations may also recognize the time zone names of the IANA time zone database, such as "Asia/Shanghai", "Asia/Kolkata", "America/New_York".
hour12
Whether to use 12-hour time (as opposed to 24-hour time). Possible values are true and false; the default is locale dependent.
formatMatcher
The format matching algorithm to use. Possible values are "basic" and "best fit"; the default is "best fit". See the following paragraphs for information about the use of this property.

The following properties describe the date-time components to use in formatted output, and their desired representations. Implementations are required to support at least the following subsets:

  • weekday, year, month, day, hour, minute, second
  • weekday, year, month, day
  • year, month, day
  • year, month
  • month, day
  • hour, minute, second
  • hour, minute

Implementations may support other subsets, and requests will be negotiated against all available subset-representation combinations to find the best match. Two algorithms are available for this negotiation and selected by the formatMatcher property: A fully specified "basic" algorithm and an implementation dependent "best fit" algorithm.

weekday
The representation of the weekday. Possible values are "narrow", "short", "long".
era
The representation of the era. Possible values are "narrow", "short", "long".
year
The representation of the year. Possible values are "numeric", "2-digit".
month
The representation of the month. Possible values are "numeric", "2-digit", "narrow", "short", "long".
day
The representation of the day. Possible values are "numeric", "2-digit".
hour
The representation of the hour. Possible values are "numeric", "2-digit".
minute
The representation of the minute. Possible values are "numeric", "2-digit".
second
The representation of the second. Possible values are "numeric", "2-digit".
timeZoneName
The representation of the time zone name. Possible values are "short", "long".

The default value for each date-time component property is {{jsxref("Global_Objects/undefined", "undefined")}}, but if all component properties are {{jsxref("Global_Objects/undefined", "undefined")}}, then year, month, and day are assumed to be "numeric".

Description

Properties

{{jsxref("DateTimeFormat.prototype", "Intl.DateTimeFormat.prototype")}}
Allows the addition of properties to all objects.

Methods

{{jsxref("DateTimeFormat.supportedLocalesOf", "Intl.DateTimeFormat.supportedLocalesOf()")}}
Returns an array containing those of the provided locales that are supported without having to fall back to the runtime's default locale.

DateTimeFormat instances

Properties

DateTimeFormat instances inherit the following properties from their prototype:

{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/prototype', 'Properties')}}

Methods

DateTimeFormat instances inherit the following methods from their prototype:

{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/prototype', 'Methods')}}

Examples

Example: Using DateTimeFormat

In basic use without specifying a locale, DateTimeFormat uses the default locale and default options.

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

Example: Using locales

This example shows some of the variations in localized date and time formats. In order to get the format of the language used in the user interface of your application, make sure to specify that language (and possibly some fallback languages) using the locales argument:

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"

Example: Using options

The date and time formats can be customized using the options argument:

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"

Specifications

Specification Status Comment
{{SpecName('ES Int 1.0', '#sec-12.1', 'Intl.DateTimeFormat')}} {{Spec2('ES Int 1.0')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatChrome("24")}} {{CompatGeckoDesktop("29")}} {{CompatIE("11")}} {{CompatOpera("15")}} {{CompatNo}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatChrome("26")}} {{CompatNo}}
{{bug("864843")}}
{{CompatNo}} {{CompatNo}} {{CompatNo}}

See also

{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl', 'See_also')}}

Revision Source

<div>{{JSRef("Global_Objects", "DateTimeFormat", "Intl,Collator,NumberFormat")}}</div>

<h2 id="Summary">Summary</h2>
<p>The <strong><code>Intl.DateTimeFormat</code></strong> object is a constructor for objects that enable language sensitive date and time formatting.</p>

<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox"><code>new Intl.DateTimeFormat([<var>locales</var>[, <var>options</var>]])
Intl.DateTimeFormat.call(<var>this</var>[, <var>locales</var>[, <var>options</var>]])</code></pre>

<h3 id="Parameters">Parameters</h3>
<dl>
 <dt><code>locales</code></dt>
 <dd>
  <p>Optional. A string with a BCP 47 language tag, or an array of such strings. For the general form and interpretation of the <code>locales</code> argument, see the {{jsxref("Global_Objects/Intl", "Intl page", "#Locale_identification_and_negotiation", 1)}}. The following Unicode extension keys are allowed:</p>
  <dl>
   <dt><code>nu</code></dt>
   <dd>Numbering system. Possible values include: <code>"arab"</code>, <code>"arabext"</code>, <code>"bali"</code>, <code>"beng"</code>, <code>"deva"</code>, <code>"fullwide"</code>, <code>"gujr"</code>, <code>"guru"</code>, <code>"hanidec"</code>, <code>"khmr"</code>, <code>"knda"</code>, <code>"laoo"</code>, <code>"latn"</code>, <code>"limb"</code>, <code>"mlym"</code>, <code>"mong"</code>, <code>"mymr"</code>, <code>"orya"</code>, <code>"tamldec"</code>, <code>"telu"</code>, <code>"thai"</code>, <code>"tibt"</code>.</dd>
   <dt><code>ca</code></dt>
   <dd>Calendar. Possible values include: <code>"buddhist"</code>, <code>"chinese"</code>, <code>"coptic"</code>, <code>"ethioaa"</code>, <code>"ethiopic"</code>, <code>"gregory"</code>, <code>"hebrew"</code>, <code>"indian"</code>, <code>"islamic"</code>, <code>"islamicc"</code>, <code>"iso8601"</code>, <code>"japanese"</code>, <code>"persian"</code>, <code>"roc"</code>.</dd>
  </dl>
 </dd>
 <dt><code>options</code></dt>
 <dd>
  <p>Optional. An object with some or all of the following properties:</p>
  <dl>
   <dt><code>localeMatcher</code></dt>
   <dd>The locale matching algorithm to use. Possible values are <code>"lookup"</code> and <code>"best fit"</code>; the default is <code>"best fit"</code>. For information about this option, see the {{jsxref("Global_Objects/Intl", "Intl page", "#Locale_negotiation", 1)}}.</dd>
   <dt><code>timeZone</code></dt>
   <dd>The time zone to use. The only value implementations must recognize is <code>"UTC"</code>; the default is the runtime's default time zone. Implementations may also recognize the time zone names of the <a href="https://www.iana.org/time-zones">IANA time zone database</a>, such as <code>"Asia/Shanghai"</code>, <code>"Asia/Kolkata"</code>, <code>"America/New_York"</code>.</dd>
   <dt><code>hour12</code></dt>
   <dd>Whether to use 12-hour time (as opposed to 24-hour time). Possible values are <code>true</code> and <code>false</code>; the default is locale dependent.</dd>
   <dt><code>formatMatcher</code></dt>
   <dd>The format matching algorithm to use. Possible values are <code>"basic"</code> and <code>"best fit"</code>; the default is <code>"best fit"</code>. See the following paragraphs for information about the use of this property.</dd>
  </dl>
  <p>The following properties describe the date-time components to use in formatted output, and their desired representations. Implementations are required to support at least the following subsets:</p>
  <ul>
   <li><code>weekday</code>, <code>year</code>, <code>month</code>, <code>day</code>, <code>hour</code>, <code>minute</code>, <code>second</code></li>
   <li><code>weekday</code>, <code>year</code>, <code>month</code>, <code>day</code></li>
   <li><code>year</code>, <code>month</code>, <code>day</code></li>
   <li><code>year</code>, <code>month</code></li>
   <li><code>month</code>, <code>day</code></li>
   <li><code>hour</code>, <code>minute</code>, <code>second</code></li>
   <li><code>hour</code>, <code>minute</code></li>
  </ul>
  <p>Implementations may support other subsets, and requests will be negotiated against all available subset-representation combinations to find the best match. Two algorithms are available for this negotiation and selected by the <code>formatMatcher</code> property: A <a href="https://www.ecma-international.org/ecma-402/1.0/#BasicFormatMatcher">fully specified <code>"basic"</code> algorithm</a> and an implementation dependent <code>"best fit"</code> algorithm.</p>
  <dl>
   <dt><code>weekday</code></dt>
   <dd>The representation of the weekday. Possible values are <code>"narrow"</code>, <code>"short"</code>, <code>"long"</code>.</dd>
   <dt><code>era</code></dt>
   <dd>The representation of the era. Possible values are <code>"narrow"</code>, <code>"short"</code>, <code>"long"</code>.</dd>
   <dt><code>year</code></dt>
   <dd>The representation of the year. Possible values are <code>"numeric"</code>, <code>"2-digit"</code>.</dd>
   <dt><code>month</code></dt>
   <dd>The representation of the month. Possible values are <code>"numeric"</code>, <code>"2-digit"</code>, <code>"narrow"</code>, <code>"short"</code>, <code>"long"</code>.</dd>
   <dt><code>day</code></dt>
   <dd>The representation of the day. Possible values are <code>"numeric"</code>, <code>"2-digit"</code>.</dd>
   <dt><code>hour</code></dt>
   <dd>The representation of the hour. Possible values are <code>"numeric"</code>, <code>"2-digit"</code>.</dd>
   <dt><code>minute</code></dt>
   <dd>The representation of the minute. Possible values are <code>"numeric"</code>, <code>"2-digit"</code>.</dd>
   <dt><code>second</code></dt>
   <dd>The representation of the second. Possible values are <code>"numeric"</code>, <code>"2-digit"</code>.</dd>
   <dt><code>timeZoneName</code></dt>
   <dd>The representation of the time zone name. Possible values are <code>"short"</code>, <code>"long"</code>.</dd>
  </dl>
  <p class="noinclude">The default value for each date-time component property is {{jsxref("Global_Objects/undefined", "undefined")}}, but if all component properties are {{jsxref("Global_Objects/undefined", "undefined")}}, then <code>year</code>, <code>month</code>, and <code>day</code> are assumed to be <code>"numeric"</code>.</p>
 </dd>
</dl>

<h2 id="Description" name="Description">Description</h2>

<h3 id="Properties" name="Properties">Properties</h3>
<dl>
 <dt>{{jsxref("DateTimeFormat.prototype", "Intl.DateTimeFormat.prototype")}}</dt>
 <dd>Allows the addition of properties to all objects.</dd>
</dl>

<h3 id="Methods">Methods</h3>
<dl>
 <dt>{{jsxref("DateTimeFormat.supportedLocalesOf", "Intl.DateTimeFormat.supportedLocalesOf()")}}</dt>
 <dd>Returns an array containing those of the provided locales that are supported without having to fall back to the runtime's default locale.</dd>
</dl>

<h2 id="DateTimeFormat_instances"><code>DateTimeFormat</code> instances</h2>

<h3 id="Properties_of_DateTimeFormat_instance">Properties</h3>
<p><code>DateTimeFormat</code> instances inherit the following properties from their prototype:</p>
<div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/prototype', 'Properties')}}</div>

<h3 id="Methods_of_DateTimeFormat_instance">Methods</h3>
<p><code>DateTimeFormat</code> instances inherit the following methods from their prototype:</p>
<div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/prototype', 'Methods')}}</div>

<h2 id="Examples">Examples</h2>

<h3 id="Example:_Using_DateTimeFormat" name="Example:_Using_DateTimeFormat">Example: Using <code>DateTimeFormat</code></h3>
<p>In basic use without specifying a locale, <code>DateTimeFormat</code> uses the default locale and default options.</p>
<pre class="brush: js">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
</pre>

<h3 id="Example:_Using_locales" name="Example:_Using_locales">Example: Using <code>locales</code></h3>
<p>This example shows some of the variations in localized date and time formats. In order to get the format of the language used in the user interface of your application, make sure to specify that language (and possibly some fallback languages) using the <code>locales</code> argument:</p>
<pre class="brush: js">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));
// → "<span dir="rtl">٢٠‏/١٢‏/٢٠١٢</span>"

// 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"
</pre>

<h3 id="Example:_Using_options" name="Example:_Using_options">Example: Using <code>options</code></h3>
<p>The date and time formats can be customized using the <code>options</code> argument:</p>
<pre class="brush: js">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"
</pre>


<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES Int 1.0', '#sec-12.1', 'Intl.DateTimeFormat')}}</td>
   <td>{{Spec2('ES Int 1.0')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari (WebKit)</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{CompatChrome("24")}}</td>
    <td>{{CompatGeckoDesktop("29")}}</td>
    <td>{{CompatIE("11")}}</td>
    <td>{{CompatOpera("15")}}</td>
    <td>{{CompatNo}}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Phone</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatChrome("26")}}</td>
    <td>
     {{CompatNo}}<br>
     {{bug("864843")}}
    </td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
   </tr>
  </tbody>
 </table>
</div>

<h2 id="See_also">See also</h2>
<div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl', 'See_also')}}</div>
Revert to this revision