toLocaleString (Date)

Converts a date to a string by using the current or specified locale.

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

dateObj

Required. The Date object to convert.

locales

Optional. An array of locale strings that contain one or more language or locale tags. If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale. If you omit this parameter, the default locale of the JavaScript runtime is used.

options

Optional. An object that contains one or more properties that specify comparison options.

Starting in Internet Explorer 11, toLocaleString uses Intl.DateTimeFormat internally to make comparisons, which adds support for the locales and options parameters. For more information about these parameters, see Intl.DateTimeFormat.

Important note Important

The locales and options parameters are not supported in all document modes and browser versions. For more information, see the Requirements section.

When you use toLocaleString in Internet Explorer 10 standards document mode, earlier document modes, and quirks mode:

  • It returns a String object that contains the date written in the current locale's long default format.

  • For dates between 1601 and 1999 A.D., the date is formatted according to the user's Control Panel regional settings.

Note Note

If you omit the locales parameter, use toLocaleString only to display results to a user; never use it to compute values within a script, because the returned result is machine-specific.

The following example shows how to use the toLocaleString method.

function toLocaleStrDemo(){   
   var d, s;                      //Declare variables.
   d = new Date();                //Create Date object.
   s = "Current setting is ";
   s += d.toLocaleString();       //Convert to current locale.
   return(s);                     //Return converted date
}

The following example shows how to use the toLocaleString method with a specified locale and comparison options.

var date = new Date(Date.UTC(2013, 1, 1, 14, 0, 0));
var options = { weekday: "long", year: "numeric", month: "short",
    day: "numeric" };

// Using I18N toLocaleString
document.write(date.toLocaleString("en-US"));
document.write(date.toLocaleString("ja-JP"));
document.write(date.toLocaleString("ar-SA", options));
document.write(date.toLocaleString("hi-IN", options));

// Output:
// ‎2‎/‎1‎/‎2013‎ ‎6‎:‎00‎:‎00‎ ‎AM
// ‎2013‎年‎2‎月‎1‎日‎ ‎6‎:‎00‎:‎00
// ‏الجمعة‏, ‏٢٠‏ ‏ربيع الأول‏, ‏١٤٣٤
// ‎शुक्रवार‎, ‎01‎ ‎फरवरी‎ ‎2013

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Also supported in Store apps (Windows 8 and Windows Phone 8.1). See Version Information.

locales and options parameters:

Supported in the Internet Explorer 11 standards document mode. Also supported in Store apps (Windows 8.1 and Windows Phone 8.1). See Version Information. Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards. Not supported in Windows 8.

Show:
© 2014 Microsoft