toLocaleDateString() method returns a string with a language sensitive representation of the date portion of this date. The new locales and options arguments let applications specify the language whose formatting conventions should be used and allow to customize the behavior of the function. In older implementations, which ignore the locales and options arguments, the locale used and the form of the string returned are entirely implementation dependent.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

句法

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

参数

locales and options arguments customize the behavior of the function and let applications specify the language whose formatting conventions should be used. In implementations, which ignore the locales and options arguments, the locale used and the form of the string returned are entirely implementation dependent.

Intl.DateTimeFormat() 构造函数 for details on these parameters and how to use them.

The default value for each date-time component property is undefined , but if the weekday , year , month , day properties are all undefined , then year , month ,和 day are assumed to be "numeric" .

返回值

A string representing the date portion of the given Date instance according to language-specific conventions.

Performance

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

范例

使用 toLocaleDateString()

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));
// toLocaleDateString() without arguments depends on the implementation,
// the default locale, and the default time zone
console.log(date.toLocaleDateString());
// → "12/11/2012" if run in en-US locale with time zone America/Los_Angeles
					

Checking for support for locales and options arguments

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 toLocaleDateStringSupportsLocales() {
  try {
    new Date().toLocaleDateString('i');
  } catch (e) {
    return e.name === 'RangeError';
  }
  return false;
}
					

使用 locales

This example shows some of the variations in localized date 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(date.toLocaleDateString('en-US'));
// → "12/19/2012"
// British English uses day-month-year order
console.log(date.toLocaleDateString('en-GB'));
// → "20/12/2012"
// Korean uses year-month-day order
console.log(date.toLocaleDateString('ko-KR'));
// → "2012. 12. 20."
// Event for Persian, It's hard to manually convert date to Solar Hijri
console.log(date.toLocaleDateString('fa-IR'));
// → "۱۳۹۱/۹/۳۰"
// Arabic in most Arabic speaking countries uses real Arabic digits
console.log(date.toLocaleDateString('ar-EG'));
// → "٢٠‏/١٢‏/٢٠١٢"
// for Japanese, applications may want to use the Japanese calendar,
// where 2012 was the year 24 of the Heisei era
console.log(date.toLocaleDateString('ja-JP-u-ca-japanese'));
// → "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(date.toLocaleDateString(['ban', 'id']));
// → "20/12/2012"
					

使用 options

The results provided by toLocaleDateString() 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(date.toLocaleDateString('de-DE', options));
// → "Donnerstag, 20. Dezember 2012"
// an application may want to use UTC and make that visible
options.timeZone = 'UTC';
options.timeZoneName = 'short';
console.log(date.toLocaleDateString('en-US', options));
// → "Thursday, December 20, 2012, GMT"
					

规范

规范
ECMAScript (ECMA-262)
The definition of 'Date.prototype.toLocaleDateString' in that specification.
ECMAScript 国际化 API (ECMA-402)
The definition of 'Date.prototype.toLocaleDateString' in that specification.

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
toLocaleDateString Chrome 1 Edge 12 Firefox 1 IE 5.5 Opera 5 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs 0.1.100
IANA time zone names in timeZone option Chrome 24 Edge 14 Firefox 52 IE No Opera 15 Safari 6.1 WebView Android 4.4 Chrome Android 25 Firefox Android No Opera Android 14 Safari iOS 7 Samsung Internet Android 1.5 nodejs 0.12
locales Chrome 24 Edge 12 Firefox 29 IE 11 Opera 15 Safari 10 WebView Android 4.4 Chrome Android 25 Firefox Android 56 Opera Android 14 Safari iOS 10 Samsung Internet Android 1.5 nodejs 13.0.0
13.0.0
部分支持 0.12
Before version 13.0.0, only the locale data for en-US is available by default. When other locales are specified, the function silently falls back to en-US . To make full ICU (locale) data available for versions prior to 13, see Node.js documentation on the --with-intl option and how to provide the data.
options Chrome 24 Edge 12 Firefox 29 IE 11 Opera 15 Safari 10 WebView Android 4.4 Chrome Android 25 Firefox Android 56 Opera Android 14 Safari iOS 10 Samsung Internet Android 1.5 nodejs 0.12

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

另请参阅

元数据

  • 最后修改:
  1. 标准内置对象
  2. Date
  3. 方法
    1. Date.UTC()
    2. Date.now()
    3. Date.parse()
    4. Date.prototype.getDate()
    5. Date.prototype.getDay()
    6. Date.prototype.getFullYear()
    7. Date.prototype.getHours()
    8. Date.prototype.getMilliseconds()
    9. Date.prototype.getMinutes()
    10. Date.prototype.getMonth()
    11. Date.prototype.getSeconds()
    12. Date.prototype.getTime()
    13. Date.prototype.getTimezoneOffset()
    14. Date.prototype.getUTCDate()
    15. Date.prototype.getUTCDay()
    16. Date.prototype.getUTCFullYear()
    17. Date.prototype.getUTCHours()
    18. Date.prototype.getUTCMilliseconds()
    19. Date.prototype.getUTCMinutes()
    20. Date.prototype.getUTCMonth()
    21. Date.prototype.getUTCSeconds()
    22. Date.prototype.getYear()
    23. Date.prototype.setDate()
    24. Date.prototype.setFullYear()
    25. Date.prototype.setHours()
    26. Date.prototype.setMilliseconds()
    27. Date.prototype.setMinutes()
    28. Date.prototype.setMonth()
    29. Date.prototype.setSeconds()
    30. Date.prototype.setTime()
    31. Date.prototype.setUTCDate()
    32. Date.prototype.setUTCFullYear()
    33. Date.prototype.setUTCHours()
    34. Date.prototype.setUTCMilliseconds()
    35. Date.prototype.setUTCMinutes()
    36. Date.prototype.setUTCMonth()
    37. Date.prototype.setUTCSeconds()
    38. Date.prototype.setYear()
    39. Date.prototype.toDateString()
    40. Date.prototype.toGMTString()
    41. Date.prototype.toISOString()
    42. Date.prototype.toJSON()
    43. Date.prototype.toLocaleDateString()
    44. Date.prototype.toLocaleString()
    45. Date.prototype.toLocaleTimeString()
    46. Date.prototype.toSource()
    47. Date.prototype.toString()
    48. Date.prototype.toTimeString()
    49. Date.prototype.toUTCString()
    50. Date.prototype.valueOf()
    51. Date.prototype[@@toPrimitive]
  4. 继承:
  5. Function
  6. 特性
    1. Function.arguments
    2. Function.caller
    3. Function.displayName
    4. Function.length
    5. Function.name
  7. 方法
    1. Function.prototype.apply()
    2. Function.prototype.bind()
    3. Function.prototype.call()
    4. Function.prototype.toSource()
    5. Function.prototype.toString()
  8. Object
  9. 特性
    1. Object.prototype.__proto__
    2. Object.prototype.constructor
  10. 方法
    1. Object.prototype.__defineGetter__()
    2. Object.prototype.__defineSetter__()
    3. Object.prototype.__lookupGetter__()
    4. Object.prototype.__lookupSetter__()
    5. Object.prototype.hasOwnProperty()
    6. Object.prototype.isPrototypeOf()
    7. Object.prototype.propertyIsEnumerable()
    8. Object.prototype.toLocaleString()
    9. Object.prototype.toSource()
    10. Object.prototype.toString()
    11. Object.prototype.valueOf()
    12. Object.setPrototypeOf()