Intl.DateTimeFormat.prototype.formatToParts()
method allows locale-aware formatting of strings produced by
DateTimeFormat
formatters.
dateTimeFormat.formatToParts(date)
date
可选
The date to format.
Array
of objects containing the formatted date in parts.
formatToParts()
method is useful for custom formatting of date strings. It returns an
Array
of objects containing the locale-specific tokens from which it possible to build custom strings while preserving the locale-specific parts. The structure the
formatToParts()
method returns, looks like this:
[
{ type: 'day', value: '17' },
{ type: 'weekday', value: 'Monday' }
]
Possible types are the following:
"17"
.
"AM",
"PM"
,
"in the morning"
,或
"noon"
"BC"
or
"AD"
.
"0"
or
"00"
or
"000"
.
"3"
or
"03"
.
"/"
,
","
,
"o'clock"
,
"de"
,等。
"00"
.
"12"
.
"2019"
.
"07"
or
"42"
.
"UTC"
.
"M"
,
"Monday"
,或
"Montag"
.
"2012"
or
"96"
.
"geng-zi"
A polyfill for this feature is available in the proposal repository .
DateTimeFormat
outputs localized, opaque strings that cannot be manipulated directly:
var date = Date.UTC(2012, 11, 17, 3, 0, 42);
var formatter = new Intl.DateTimeFormat('en-us', {
weekday: 'long',
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
fractionalSecondDigits: 3,
hour12: true,
timeZone: 'UTC'
});
formatter.format(date);
// "Monday, 12/17/2012, 3:00:42.000 AM"
However, in many User Interfaces there is a desire to customize the formatting of this string. The
formatToParts
method enables locale-aware formatting of strings produced by
DateTimeFormat
formatters by providing you the string in parts:
formatter.formatToParts(date);
// return value:
[
{ type: 'weekday', value: 'Monday' },
{ type: 'literal', value: ', ' },
{ type: 'month', value: '12' },
{ type: 'literal', value: '/' },
{ type: 'day', value: '17' },
{ type: 'literal', value: '/' },
{ type: 'year', value: '2012' },
{ type: 'literal', value: ', ' },
{ type: 'hour', value: '3' },
{ type: 'literal', value: ':' },
{ type: 'minute', value: '00' },
{ type: 'literal', value: ':' },
{ type: 'second', value: '42' },
{ type: 'fractionalSecond', value: '000' },
{ type: 'literal', value: ' ' },
{ type: 'dayPeriod', value: 'AM' }
]
Now the information is available separately and it can be formatted and concatenated again in a customized way. For example by using
Array.prototype.map()
,
箭头函数
,
switch statement
,
template literals
,和
Array.prototype.reduce()
.
var dateString = formatter.formatToParts(date).map(({type, value}) => {
switch (type) {
case 'dayPeriod': return `<b>${value}</b>`;
default : return value;
}
}).reduce((string, part) => string + part);
This will make the day period bold, when using the
formatToParts()
方法。
console.log(formatter.format(date)); // "Monday, 12/17/2012, 3:00:42.000 AM" console.log(dateString); // "Monday, 12/17/2012, 3:00:42.000 <b>AM</b>"
In some cases, calendars use named years. Chinese and Tibetan calendars, for example, use a 60-year
sexagenary cycle
of named years. These years are disambiguated by relationship to corresponding years on the Gregorian calendar. When this is the case, the result of
formatToParts()
will contain an entry for
relatedYear
when a year would normally be present, containing the 4-digit Gregorian year, instead of an entry for
year
. Setting an entry in the bag for
year
(with any value) will yield both the and the
yearName
Gregorian
relatedYear:
let opts = { year: "numeric", month: "numeric", day: "numeric" };
let df = new Intl.DateTimeFormat("zh-u-ca-chinese", opts);
df.formatToParts(Date.UTC(2012, 11, 17, 3, 0, 42));
// return value
[
{ type: 'relatedYear', value: '2012' },
{ type: 'literal', value: '年' },
{ type: 'month', value: '十一月' },
{ type: 'day', value: '4' }
]
若
year
option is not set in the bag (to any value), the result will include only the
relatedYear
:
let df = new Intl.DateTimeFormat("zh-u-ca-chinese");
df.formatToParts(Date.UTC(2012, 11, 17, 3, 0, 42));
// return value
[
{ type: 'relatedYear', value: '2012' },
{ type: 'literal', value: '年' },
{ type: 'month', value: '十一月' },
{ type: 'day', value: '4' }
]
In cases where the
year
would be output,
.format()
may commonly present these side-by-side:
let df = new Intl.DateTimeFormat("zh-u-ca-chinese", {year: "numeric"});
df.format(Date.UTC(2012, 11, 17, 3, 0, 42));
// return value
2012壬辰年
This also makes it possible to mix locale and calendar in both
format
:
let df = new Intl.DateTimeFormat("en-u-ca-chinese", {year: "numeric"});
let date = Date.UTC(2012, 11, 17, 3, 0, 42);
df.format(date);
// return value
2012(ren-chen)
And
formatToParts
:
let opts = {month: 'numeric', day: 'numeric', year: "numeric"};
let df = new Intl.DateTimeFormat("en-u-ca-chinese", opts);
let date = Date.UTC(2012, 11, 17, 3);
df.formatToParts(date)
// return value
[
{ type: 'month', value: '11' },
{ type: 'literal', value: '/' },
{ type: 'day', value: '4' },
{ type: 'literal', value: '/' },
{ type: 'relatedYear', value: '2012' }
]
| 规范 |
|---|
|
ECMAScript 国际化 API (ECMA-402)
The definition of 'Intl.DateTimeFormat.prototype.formatToParts' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
formatToParts
|
Chrome
57
|
Edge 18 | Firefox 51 | IE No |
Opera
44
|
Safari 11 |
WebView Android
57
|
Chrome Android
57
|
Firefox Android 56 |
Opera Android
43
|
Safari iOS 11 |
Samsung Internet Android
7.0
|
nodejs
8.0.0
|
完整支持
不支持
见实现注意事项。
Intl.DateTimeFormat
Intl.DateTimeFormat.prototype.format
Date.prototype.toLocaleString()
Date.prototype.toLocaleDateString()
Date.prototype.toLocaleTimeString()
Intl
Collator
DateTimeFormat
ListFormat
NumberFormat
PluralRules
RelativeTimeFormat
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()