JS Intl.DateTimeFormat format()

🌙
手机阅读
本文目录结构

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

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.

Syntax

dateTimeFormat.format(date)

Parameters

date

The date to format.

Description

The format getter formats a date into a string according to the locale and formatting options of this Intl.DateTimeFormat object.

Examples

Using format

Use the 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."

Using format with map

Use the 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

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

Specifications

Specification Status Comment
[ECMAScript Internationalization API 1.0 (ECMA-402)
The definition of ‘Intl.DateTimeFormat.format’ in that specification.](https://www.ecma-international.org/ecma-402/1.0/#sec-12.3.2) Standard Initial definition.
[ECMAScript Internationalization API 2.0 (ECMA-402)
The definition of ‘Intl.DateTimeFormat.format’ in that specification.](https://www.ecma-international.org/ecma-402/2.0/#sec-12.3.2) Standard  
[ECMAScript Internationalization API 4.0 (ECMA-402)
The definition of ‘Intl.DateTimeFormat.format’ in that specification.](https://tc39.es/ecma402/#sec-Intl.DateTimeFormat.prototype.format) Draft  

See also


AXIHE / 精选资源

浏览全部教程

面试题

学习网站

前端培训
自己甄别

前端书籍

关于朱安邦

我叫 朱安邦,阿西河的站长,在杭州。

以前是一名平面设计师,后来开始接接触前端开发,主要研究前端技术中的JS方向。

业余时间我喜欢分享和交流自己的技术,欢迎大家关注我的 Bilibili

关注我: Github / 知乎

目前重心已经放在研究区块链上面了

我叫朱安邦,阿西河的站长

目前在杭州从事区块链周边的开发工作,机械专业,以前从事平面设计工作。

2014年底脱产在老家自学6个月的前端技术,自学期间几乎从未出过家门,最终找到了满意的前端工作。更多>