From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../intl/relativetimeformat/index.html | 165 +++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/intl/relativetimeformat/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/intl/relativetimeformat') diff --git a/files/zh-cn/web/javascript/reference/global_objects/intl/relativetimeformat/index.html b/files/zh-cn/web/javascript/reference/global_objects/intl/relativetimeformat/index.html new file mode 100644 index 0000000000..561af8a1fe --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/intl/relativetimeformat/index.html @@ -0,0 +1,165 @@ +--- +title: Intl.RelativeTimeFormat +slug: Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat +translation_of: Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat +--- +
+
{{JSRef}}
+ +

Intl.RelativeTimeFormat对象启用本地化的相对时间格式。

+ +
{{EmbedInteractiveExample("pages/js/intl-relativetimeformat.html")}}
+ + + +

句法

+ +
new Intl.RelativeTimeFormat([locales[, options]])
+
+ +

参数

+ +
+
locales
+
+

可选的。带有BCP 47语言标记的字符串,或此类字符串的数组。有关参数的一般形式和解释locales,请参阅{{jsxref("Global_Objects/Intl","Intl page","#Locale_identification_and_negotiation",1)}}。

+
+
options
+
可选的。具有以下部分或全部属性的对象: +
    +
  • localeMatcher
    + 要使用的区域设置匹配算法。可能的值是"lookup""best fit"; 默认是"best fit"。有关此选项的信息,请参阅Intl
  • +
  • numeric
    + 输出消息的格式。可能的值是: +
      +
    • "always"(默认,例如,1 day ago),
    • +
    • "auto"(例如yesterday)。该"auto"值允许不必总是在输出中使用数值。
    • +
    +
  • +
  • style
    + 国际化信息的长度。可能的值是: +
      +
    • "long"(默认,例如,in 1 month)
    • +
    • "short"(例如in 1 mo.),
    • +
    • "narrow"(例如in 1 mo.)。狭窄的风格可能类似于某些语言环境的短风格。
    • +
    +
  • +
+
+
+ +

描述

+ +

属性

+ +
+
{{jsxref("RelativeTimeFormat.prototype","Intl.RelativeTimeFormat.prototype")}}
+
允许向所有对象添加属性。
+
+ +

方法

+ +
+
{{jsxref("RelativeTimeFormat.supportedLocalesOf","Intl.RelativeTimeFormat.supportedLocalesOf()")}}
+
返回一个数组,其中包含所支持的语言环境,而不必回退到运行时的默认语言环境。
+
+ +

RelativeTimeFormat 实例

+ +

属性

+ +

RelativeTimeFormat 实例从其原型继承以下属性:

+ +

{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/RelativeTimeFormat/prototype','Properties')}}

+ +

方法

+ +

RelativeTimeFormat 实例从其原型继承以下方法:

+ +

{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/RelativeTimeFormat/prototype','Methods')}}

+ +

例子

+ +

基本format用法

+ +

以下示例显示如何使用英语创建相对时间格式化程序。

+ +
//在语言环境中创建相对时间格式化程序
+//显式传入默认值。
+const rtf = new Intl.RelativeTimeFormat("en",{
+    localeMatcher: "bestfit",//其他值:"lookup"
+    numeric: "always",//其他值:"auto"
+    style: "long",//其他值:"short"或"narrow"
+});
+
+//使用负值(-1)格式化相对时间。
+rtf.format(-1,"day");
+//>"1 day ago"
+
+//使用正值(1)格式化相对时间。
+rtf.format(1,"day");
+//>"in 1 day"
+ +

使用auto选项

+ +

如果numeric:auto选项被传递,它将生成字符串yesterdaytomorrow代替1 day agoin 1 day。这允许不必总是在输出中使用数值。

+ +
//在语言环境中创建相对时间格式化程序
+//使用数字:传入"auto"选项值。
+const rtf = new Intl.RelativeTimeFormat("en",{numeric: "auto"});
+
+//使用负值(-1)格式化相对时间。
+rtf.format(-1,"day");
+//>"yesterday"
+
+//使用正日单位(1)格式化相对时间。
+rtf.format(1,"day");
+//>"tomorrow"
+
+ +

运用 formatToParts

+ +

以下示例显示如何创建返回格式化部件的相对时间格式器

+ +
const rtf = new Intl.RelativeTimeFormat("en",{numeric: "auto"});
+
+//使用日期单位格式化相对时间。
+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"}]
+
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
Intl.RelativeTimeFormat ConstructorStage 3
+ +

Browser compatibility

+ +
+ + +

{{Compat("javascript.builtins.Intl.RelativeTimeFormat")}}

+ +

See also

+ + +
-- cgit v1.2.3-54-g00ecf