aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/intl/relativetimeformat/index.html
blob: d8206fe2633d7ce0f73250a33485c41d6ce4ed7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
---
title: Intl.RelativeTimeFormat
slug: Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat
translation_of: Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat
---
<div>
<div>{{JSRef}}</div>

<p><strong><code>Intl.RelativeTimeFormat</code></strong>对象启用本地化的相对时间格式。</p>

<div>{{EmbedInteractiveExample("pages/js/intl-relativetimeformat.html")}}</div>

<h2 id="句法">句法</h2>

<pre><code>new Intl.RelativeTimeFormat([<var>locales</var>[, <var>options</var>]])
</code></pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>locales</code></dt>
 <dd>
 <p>可选的。带有BCP 47语言标记的字符串,或此类字符串的数组。有关参数的一般形式和解释<code>locales</code>,请参阅{{jsxref("Global_Objects/Intl","Intl page","#Locale_identification_and_negotiation",1)}}</p>
 </dd>
 <dt><code>options</code></dt>
 <dd>可选的。具有以下部分或全部属性的对象:
 <ul>
  <li><code>localeMatcher</code><br>
   要使用的区域设置匹配算法。可能的值是<code>"lookup"</code><code>"best fit"</code>; 默认是<code>"best fit"</code>。有关此选项的信息,请参阅<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_negotiation" rel="nofollow"><code>Intl</code></a></li>
  <li><code>numeric</code><br>
   输出消息的格式。可能的值是:
   <ul>
    <li><code>"always"</code>(默认,例如,<code>1 day ago</code>),</li>
    <li><code>"auto"</code>(例如<code>yesterday</code>)。该<code>"auto"</code>值允许不必总是在输出中使用数值。</li>
   </ul>
  </li>
  <li><code>style</code><br>
   国际化信息的长度。可能的值是:
   <ul>
    <li><code>"long"</code>(默认,例如,<code>in 1 month</code>)</li>
    <li><code>"short"</code>(例如<code>in 1 mo.</code>),</li>
    <li><code>"narrow"</code>(例如<code>in 1 mo.</code>)。狭窄的风格可能类似于某些语言环境的短风格。</li>
   </ul>
  </li>
 </ul>
 </dd>
</dl>

<h2 id="描述">描述</h2>

<h3 id="属性">属性</h3>

<dl>
 <dt>{{jsxref("RelativeTimeFormat.prototype","Intl.RelativeTimeFormat.prototype")}}</dt>
 <dd>允许向所有对象添加属性。</dd>
</dl>

<h3 id="方法">方法</h3>

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

<h2 id="RelativeTimeFormat_实例"><code>RelativeTimeFormat</code> 实例</h2>

<h3 id="属性_2">属性</h3>

<p><code>RelativeTimeFormat</code> 实例从其原型继承以下属性:</p>

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

<h3 id="方法_2">方法</h3>

<p><code>RelativeTimeFormat</code> 实例从其原型继承以下方法:</p>

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

<h2 id="例子">例子</h2>

<h3 id="基本format用法">基本<code>format</code>用法</h3>

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

<pre class="brush: js">//在语言环境中创建相对时间格式化程序
//显式传入默认值。
const rtf = new Intl.RelativeTimeFormat("en",{
    localeMatcher: "bestfit",//其他值:"lookup"
    numeric: "always",//其他值:"auto"
    style: "long",//其他值:"short"或"narrow"
});

//使用负值(-1)格式化相对时间。
rtf.format(-1,"day");
//&gt;"1 day ago"

//使用正值(1)格式化相对时间。
rtf.format(1,"day");
//&gt;"in 1 day"</pre>

<h3 id="使用auto选项">使用<code>auto</code>选项</h3>

<p>如果<code>numeric:auto</code>选项被传递,它将生成字符串<code>yesterday</code><code>tomorrow</code>代替<code>1 day ago</code><code>in 1 day</code>。这允许不必总是在输出中使用数值。</p>

<pre class="brush: js">//在语言环境中创建相对时间格式化程序
//使用数字:传入"auto"选项值。
const rtf = new Intl.RelativeTimeFormat("en",{numeric: "auto"});

//使用负值(-1)格式化相对时间。
rtf.format(-1,"day");
//&gt;"yesterday"

//使用正日单位(1)格式化相对时间。
rtf.format(1,"day");
//&gt;"tomorrow"
</pre>

<h3 id="运用_formatToParts">运用 <code>formatToParts</code></h3>

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

<pre class="brush: js">const rtf = new Intl.RelativeTimeFormat("en",{numeric: "auto"});

//使用日期单位格式化相对时间。
rtf.formatToParts(-1,"day");
//&gt; [{type: "literal",value: "yesterday"}]

rtf.formatToParts(100,"day");
//&gt; [{type: "literal",value: "in"},
//&gt; {type: "integer",value: "100",unit: "day"},
//&gt; {type: "literal",value: "days"}]</pre>
</div>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td><a href="https://tc39.es/proposal-intl-relative-time/#sec-intl-relativetimeformat-constructor">Intl.RelativeTimeFormat Constructor</a></td>
   <td>Stage 3</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>


<p>{{Compat("javascript.builtins.Intl.RelativeTimeFormat")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="https://developers.google.com/web/updates/2018/10/intl-relativetimeformat">The Intl.RelativeTimeFormat API</a></li>
</ul>
</div>