Intl.DateTimeFormat.prototype.format() method formats a date according to the locale and formatting options of this Intl.DateTimeFormat 对象。

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.

句法

dateTimeFormat.format(date)
					

参数

date

The date to format.

描述

format getter formats a date into a string according to the locale and formatting options of this Intl.DateTimeFormat 对象。

范例

使用 format

使用 format getter function for formatting a single date, here for Serbia:

var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
var dateTimeFormat = new Intl.DateTimeFormat('sr-RS', options);
console.log(dateTimeFormat.format(new Date()));
// → "недеља, 7. април 2013."
					

使用 format with map

使用 format getter function for formatting all dates in an array. Note that the function is bound to the Intl.DateTimeFormat from which it was obtained, so it can be passed directly to Array.prototype.map() .

var a = [new Date(2012, 08), new Date(2012, 11), new Date(2012, 03)];
var options = { year: 'numeric', month: 'long' };
var dateTimeFormat = new Intl.DateTimeFormat('pt-BR', options);
var formatted = a.map(dateTimeFormat.format);
console.log(formatted.join('; '));
// → "setembro de 2012; dezembro de 2012; abril de 2012"
					

Avoid comparing formatted date values to static values

Most of the time, the formatting returned by format() is consistent. However, this might change in the future and isn't guaranteed for all the languages — output variations are by design and allowed by the specification. Most notably, the IE and Edge browsers insert bidirectional control characters around dates, so the output text will flow properly when concatenated with other text.

For this reason you cannot expect to be able to compare the results of format() to a static value:

let d = new Date("2019-01-01T00:00:00.000000Z");
let formattedDate = Intl.DateTimeFormat(undefined, {
  year: 'numeric',
  month: 'numeric',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second: 'numeric'
}).format(d);
"1.1.2019, 01:00:00" === formattedDate;
// true in Firefox and others
// false in IE and Edge
					

注意 : See also this StackOverflow thread for more details and examples.

规范

规范
ECMAScript 国际化 API (ECMA-402)
The definition of 'Intl.DateTimeFormat.format' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 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
format 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
0.12
Before version 13.0.0, only the locale data for en-US is available by default. See the DateTimeFormat() 构造函数 了解更多细节。

图例

完整支持

完整支持

见实现注意事项。

另请参阅

元数据

  • 最后修改: