Intl.RelativeTimeFormat.prototype.formatToParts()
Intl.RelativeTimeFormat 实例的 formatToParts() 方法返回 Array 个对象,表示可用于自定义区域设置感知格式的部分中的相对时间格式。
¥The formatToParts() method of Intl.RelativeTimeFormat instances returns an Array of objects representing the relative time format in parts that can be used for custom locale-aware formatting.
Try it
语法
参数
返回值
描述
¥Description
Intl.RelativeTimeFormat.prototype.formatToParts 方法是 format 方法的一个版本,它返回表示对象的 "parts" 的对象数组,将格式化数字分成其组成部分并将其与其他周围文本分开。这些对象有两个属性:输入 NumberFormat formatToParts 类型和值,该值是作为输出组件的字符串。如果 "part" 来自 NumberFormat,它将有一个单位属性,指示正在格式化的单位;作为较大框架一部分的文字不具有此属性。
¥The Intl.RelativeTimeFormat.prototype.formatToParts method is a version of the format method which it returns an array of objects which represent "parts" of the object, separating the formatted number into its constituent parts and separating it from other surrounding text. These objects have two properties: type a NumberFormat formatToParts type, and value, which is the String which is the component of the output. If a "part" came from NumberFormat, it will have a unit property which indicates the unit being formatted; literals which are part of the larger frame will not have this property.
示例
使用 formatToParts
¥Using formatToParts
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
// Format relative time using the day unit
rtf.formatToParts(-1, "day");
// [{ type: "literal", value: "yesterday"}]
rtf.formatToParts(100, "day");
// [
// { type: "literal", value: "in " },
// { type: "integer", value: "100", unit: "day" },
// { type: "literal", value: " days" }
// ]
规范
| Specification |
|---|
| ECMAScript Internationalization API Specification # sec-Intl.RelativeTimeFormat.prototype.formatToParts |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also