首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Intl.dateTimeFormat.format

Intl.DateTimeFormat.prototype.format属性返回一个getter函数,该函数根据此Intl.DateTimeFormat对象的区域设置和格式化选项来格式化日期。

句法

代码语言:javascript
复制
dateTimeFormat.format(date)

参数

date格式化的日期。

描述

formatgetter 返回的函数根据此Intl.DateTimeFormat对象的区域设置和格式化选项将日期格式化为字符串。

例子

运用 format

使用formatgetter 函数返回的函数格式化单个日期,例子:

代码语言:javascript
复制
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."

使用formatmap

使用formatgetter 返回的函数来格式化数组中的所有日期。请注意,该函数绑定到它Intl.DateTimeFormat从中获取,所以它可以直接传递给Array.prototype.map()

代码语言:javascript
复制
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"

产品规格

Specification

Status

Comment

ECMAScript Internationalization API 1.0 (ECMA-402)The definition of 'Intl.DateTimeFormat.format' in that specification.

Standard

Initial definition.

ECMAScript Internationalization API 2.0 (ECMA-402)The definition of 'Intl.DateTimeFormat.format' in that specification.

Standard

?

ECMAScript Internationalization API 4.0 (ECMA-402)The definition of 'Intl.DateTimeFormat.format' in that specification.

Draft

?

浏览器兼容性

Feature

Chrome

Edge

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic support

24

12

29 (29)

11

15

10

Feature

Android

Chrome for Android

Firefox Mobile (Gecko)

IE Phone

Opera Mobile

Safari Mobile

Basic support

No support

26

56.0 (56)

No support

No support

10

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com