aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/intl/relativetimeformat
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/intl/relativetimeformat
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/intl/relativetimeformat')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/intl/relativetimeformat/index.html165
1 files changed, 165 insertions, 0 deletions
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
+---
+<div>
+<div>{{JSRef}}</div>
+
+<p><strong><code>Intl.RelativeTimeFormat</code></strong>对象启用本地化的相对时间格式。</p>
+
+<div>{{EmbedInteractiveExample("pages/js/intl-relativetimeformat.html")}}</div>
+
+<p class="hidden">此交互式示例的源存储在GitHub存储库中。如果您想参与交互式示例项目,请克隆<a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a>并向我们​​发送拉取请求。</p>
+
+<h2 id="句法">句法</h2>
+
+<pre class="notranslate"><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 notranslate">//在语言环境中创建相对时间格式化程序
+//显式传入默认值。
+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 notranslate">//在语言环境中创建相对时间格式化程序
+//使用数字:传入"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 notranslate">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>