Date.prototype.toLocaleString()

  • Revision slug: JavaScript/Reference/Global_Objects/Date/toLocaleString
  • Revision title: Date.prototype.toLocaleString
  • Revision id: 375165
  • Created:
  • Creator: Norbert
  • Is current revision? No
  • Comment

Revision Content

Summary

Returns a string with a language sensitive representation of this date. The new locales and options arguments let applications specify the language whose formatting conventions should be used and customize the behavior of the function.

Method of Date
Implemented in JavaScript 1.0
ECMAScript Edition ECMAScript Language Specification, 3rd Edition
ECMAScript Internationalization API Specification, 1st Edition

Syntax

date.toLocaleString([locales [, options]])

Parameters

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

Examples

Example: Using toLocaleString

In basic use without specifying a locale, a formatted string in the default locale and with default options is returned.

var date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));

// toLocaleString without arguments depends on the implementation,
// the default locale, and the default time zone
date.toLocaleString();
// → "12/11/2012, 7:00:00 PM" if run in en-US locale with time zone America/Los_Angeles

Example: Checking for support for locales and options arguments

The locales and options arguments are not supported in all browsers yet. To check whether an implementation supports them already, you can use the requirement that illegal language tags are rejected with a RangeError exception:

function toLocaleStringSupportsLocales() {
    try {
        new Date().toLocaleString("i");
    } catch (e) {
        return e​.name === "RangeError";
    }
    return false;
}

Example: Using locales

This example shows some of the variations in localized number 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
alert(date.toLocaleString("en-US"));
// → "12/19/2012, 7:00:00 PM"

// British English uses day-month-year order
alert(date.toLocaleString("en-GB"));
// → "20/12/2012 03:00:00"

// Korean uses year-month-day order
alert(date.toLocaleString("ko-KR"));
// → "2012. 12. 20. 오후 12:00:00"

// Arabic in most Arabic speaking countries uses real Arabic digits
alert(date.toLocaleString("ar-EG"));
// → "٢٠‏/١٢‏/٢٠١٢ ٥:٠٠:٠٠ ص"

// for Japanese, applications may want to use the Japanese calendar,
// where 2012 was the year 24 of the Heisei era
alert(date.toLocaleString("ja-JP-u-ca-japanese"));
// → "24/12/20 12:00:00"

// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
alert(number.toLocaleString(["ban", "id"]));
// → "20/12/2012 11.00.00"

Example: Using options

The results provided by toLocaleString can be customized using the options argument:

var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));

// request a weekday along with the date
var options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};
alert(date.toLocaleString("de-DE", options));
// → "Donnerstag, 20. Dezember 2012"

// an application may want to use UTC and make that visible
options.timeZone = "UTC";
options.timeZoneName = "short";
alert(date.toLocaleString("en-US", options));
// → "Thursday, December 20, 2012, GMT"

// sometimes even the US needs 24-hour time
alert(date.toLocaleString("en-US", {hour12: false}));
// → "12/19/2012, 19:00:00"

Performance

When formatting large numbers of dates, it is better to create an Intl.DateTimeFormat object and use the function provided by its format property.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
locales and options arguments 24 {{CompatNightly("Firefox")}} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
locales and options arguments {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns a string with a language sensitive representation of this date. The new <code>locales</code> and <code>options</code> arguments let applications specify the language whose formatting conventions should be used and customize the behavior of the function.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Date" title="JavaScript/Reference/Global_Objects/Date"><code>Date</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.0</td>
    </tr>
    <tr>
      <td rowspan="2">ECMAScript Edition</td>
      <td>ECMAScript Language Specification, 3<sup>rd</sup> Edition</td>
    </tr>
    <tr>
      <td>ECMAScript Internationalization API Specification, 1<sup>st</sup> Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code>date.toLocaleString(</code><code>[locales [, options]])</code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<p>{{page('en-US/docs/JavaScript/Reference/Global_Objects/DateTimeFormat','Parameters')}}</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_toLocaleString" name="Example:_Using_toLocaleString">Example: Using <code>toLocaleString</code></h3>
<p>In basic use without specifying a locale, a formatted string in the default locale and with default options is returned.</p>
<pre class="brush:js">
var date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));

// toLocaleString without arguments depends on the implementation,
// the default locale, and the default time zone
date.toLocaleString();
// → "12/11/2012, 7:00:00 PM" if run in en-US locale with time zone America/Los_Angeles</pre>
<h3 id="Example:_Checking_for_support_for_locales_and_options_arguments" name="Example:_Checking_for_support_for_locales_and_options_arguments">Example: Checking for support for <code>locales</code> and <code>options</code> arguments</h3>
<p>The <code>locales</code> and <code>options</code> arguments are not supported in all browsers yet. To check whether an implementation supports them already, you can use the requirement that illegal language tags are rejected with a <code>RangeError</code> exception:</p>
<pre class="brush: js">
function toLocaleStringSupportsLocales() {
    try {
        new Date().toLocaleString("i");
    } catch (e) {
        return e​.name === "RangeError";
    }
    return false;
}
</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 number 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
alert(date.toLocaleString("en-US"));
// → "12/19/2012, 7:00:00 PM"

// British English uses day-month-year order
alert(date.toLocaleString("en-GB"));
// → "20/12/2012 03:00:00"

// Korean uses year-month-day order
alert(date.toLocaleString("ko-KR"));
// → "2012. 12. 20. 오후 12:00:00"

// Arabic in most Arabic speaking countries uses real Arabic digits
alert(date.toLocaleString("ar-EG"));
// → "<span dir="rtl">٢٠‏/١٢‏/٢٠١٢ ٥:٠٠:٠٠ ص</span>"

// for Japanese, applications may want to use the Japanese calendar,
// where 2012 was the year 24 of the Heisei era
alert(date.toLocaleString("ja-JP-u-ca-japanese"));
// → "24/12/20 12:00:00"

// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
alert(number.toLocaleString(["ban", "id"]));
// → "20/12/2012 11.00.00"
</pre>
<h3 id="Example:_Using_options" name="Example:_Using_options">Example: Using <code>options</code></h3>
<p>The results provided by <code>toLocaleString</code> 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 the date
var options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};
alert(date.toLocaleString("de-DE", options));
// → "Donnerstag, 20. Dezember 2012"

// an application may want to use UTC and make that visible
options.timeZone = "UTC";
options.timeZoneName = "short";
alert(date.toLocaleString("en-US", options));
// → "Thursday, December 20, 2012, GMT"

// sometimes even the US needs 24-hour time
alert(date.toLocaleString("en-US", {hour12: false}));
// → "12/19/2012, 19:00:00"
</pre>
<h2 id="Performance" name="Performance">Performance</h2>
<p>When formatting large numbers of dates, it is better to create an <a href="/en-US/docs/JavaScript/Reference/Global_Objects/DateTimeFormat" title="/en-US/docs/JavaScript/Reference/Global_Objects/DateTimeFormat"><code>Intl.DateTimeFormat</code></a> object and use the function provided by its <a href="/en-US/docs/JavaScript/Reference/Global_Objects/DateTimeFormat/format" title="/en-US/docs/JavaScript/Reference/Global_Objects/DateTimeFormat/format"><code>format</code></a> property.</p>
<h2 id="Browser_Compatibility" name="Browser_Compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<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>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
      </tr>
      <tr>
        <td><code>locales</code> and <code>options</code> arguments</td>
        <td>24</td>
        <td>{{CompatNightly("Firefox")}}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</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>Firefox Mobile (Gecko)</th>
        <th>IE Phone</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
      </tr>
      <tr>
        <td><code>locales</code> and <code>options</code> arguments</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Date/toString" title="JavaScript/Reference/Global_Objects/Date/toString">toString</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Date/toUTCString" title="JavaScript/Reference/Global_Objects/Date/toUTCString">toUTCString</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Date/toLocaleDateString" title="JavaScript/Reference/Global_Objects/Date/toLocaleDateString">toLocaleDateString</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString" title="JavaScript/Reference/Global_Objects/Date/toLocaleTimeString">toLocaleTimeString</a></li>
</ul>
Revert to this revision