mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-09 15:34:05 +08:00
61 lines
5.9 KiB
HTML
61 lines
5.9 KiB
HTML
<article id="wikiArticle">
|
||
<div></div>
|
||
<h2 id="错误提示">错误提示</h2>
|
||
<pre><code class="language-javascript">Warning: Date.prototype.toLocaleFormat is deprecated; consider using Intl.DateTimeFormat instead
|
||
</code></pre>
|
||
<h2 id="错误类型">错误类型</h2>
|
||
<p>警告。JavaScript 引擎不会停止运行。</p>
|
||
<h2 id="哪里出错了?">哪里出错了?</h2>
|
||
<p><a href="Reference/Global_Objects/Date/toLocaleFormat" title="非标准方法 toLocaleFormat() 按特定的格式将一个日期转换成一个字符串。 Intl.DateTimeFormat 是符合标准的格式化日期的替代方法。另见更新的(newer)版本的 Date.prototype.toLocaleDateString()方法."><code>Date.prototype.toLocaleFormat</code></a> 是非标准化的方法,已经被废弃,不应该再进行使用。该方法需要传入与 C 语言中的strftime() 方法相似的格式化字符串。该实现将会在 <a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=818634" rel="noopener" title="FIXED: Remove support for Date.prototype.toLocaleFormat">bug 818634</a> 中完全移除。</p>
|
||
<h2 id="示例">示例</h2>
|
||
<h3 id="废止使用的语法">废止使用的语法</h3>
|
||
<p><a href="Reference/Global_Objects/Date/toLocaleFormat" title="非标准方法 toLocaleFormat() 按特定的格式将一个日期转换成一个字符串。 Intl.DateTimeFormat 是符合标准的格式化日期的替代方法。另见更新的(newer)版本的 Date.prototype.toLocaleDateString()方法."><code>Date.prototype.toLocaleFormat</code></a> 方法已经废弃,以后会被移除(只在 Firefox 浏览器中有用到,没有在其他浏览器中使用)。</p>
|
||
<pre><code class="language-js example-bad">var today = new Date();
|
||
var date = today.toLocaleFormat('%A, %e. %B %Y');
|
||
|
||
console.log(date);
|
||
// In German locale
|
||
// "Freitag, 10. März 2017"</code></pre>
|
||
<h3 id="备选标准语法之一:使用_ECMAScript_Intl_API">备选标准语法之一:使用 ECMAScript Intl API </h3>
|
||
<p>ECMA-402 (ECMAScript Intl API) 标准规定了支持不同语言的日期和时间格式化形式的对象和方法(在 Chrome 24+, Firefox 29+, IE11+, Safari10+ 中可用)。</p>
|
||
<p>现在如果只需要格式化一个日期对象的话,那么可以使用 <a href="Reference/Global_Objects/Date/toLocaleDateString" title="toLocaleDateString() 方法返回该日期对象日期部分的字符串,该字符串格式因不同语言而不同。新增的参数 locales 和 options 使程序能够指定使用哪种语言格式化规则,允许定制该方法的表现(behavior)。在旧版本浏览器中, locales 和 options 参数被忽略,使用的语言环境和返回的字符串格式是各自独立实现的。"><code>Date.prototype.toLocaleDateString</code></a> 方法。</p>
|
||
<pre><code class="language-js example-good">var today = new Date();
|
||
var options = { weekday: 'long', year: 'numeric',
|
||
month: 'long', day: 'numeric' };
|
||
var date = today.toLocaleDateString('de-DE', options);
|
||
|
||
console.log(date);
|
||
// "Freitag, 10. März 2017"
|
||
</code></pre>
|
||
<p>或者,可以使用 <a href="Reference/Global_Objects/DateTimeFormat" title="交互示例的源代码存储在 GitHub 资源库。如果你愿意分布交互示例,请复制https://github.com/mdn/interactive-examples,并向我们发送一个pull请求。"><code>Intl.DateTimeFormat</code></a> 对象,该对象允许你将格式化器对象缓存起来,省去很多重复性的计算工作,所以格式化操作会很快。这在需要对一系列的日期对象进行格式化的时候非常有用。</p>
|
||
<pre><code class="language-js example-good">var options = { weekday: 'long', year: 'numeric',
|
||
month: 'long', day: 'numeric' };
|
||
var dateFormatter = new Intl.DateTimeFormat('de-DE', options)
|
||
|
||
var dates = [Date.UTC(2012, 11, 20, 3, 0, 0),
|
||
Date.UTC(2014, 04, 12, 8, 0, 0)];
|
||
|
||
dates.forEach(date => console.log(dateFormatter.format(date)));
|
||
|
||
// "Donnerstag, 20. Dezember 2012"
|
||
// "Montag, 12. Mai 2014"
|
||
</code></pre>
|
||
<h3 id="备选标准语法之二:使用_Date_对象的方法">备选标准语法之二:使用 Date 对象的方法</h3>
|
||
<p><a href="Reference/Date" title="此页面仍未被本地化, 期待您的翻译!"><code>Date</code></a> 对象提供了一系列的方法来生成定制化的格式化字符串。</p>
|
||
<pre><code class="language-js example-bad">(new Date()).toLocaleFormat("%Y%m%d");
|
||
// "20170310"
|
||
</code></pre>
|
||
<p>可以转换为:</p>
|
||
<pre><code class="language-js example-good">let now = new Date();
|
||
let date = now.getFullYear() * 10000 +
|
||
(now.getMonth() + 1) * 100 + now.getDate();
|
||
|
||
console.log(date);
|
||
// "20170310"</code></pre>
|
||
<h2 id="相关内容">相关内容</h2>
|
||
<ul>
|
||
<li><a href="Reference/Global_Objects/Date/toLocaleFormat" title="非标准方法 toLocaleFormat() 按特定的格式将一个日期转换成一个字符串。 Intl.DateTimeFormat 是符合标准的格式化日期的替代方法。另见更新的(newer)版本的 Date.prototype.toLocaleDateString()方法."><code>Date.prototype.toLocaleFormat</code></a></li>
|
||
<li><a href="Reference/Global_Objects/Date/toLocaleDateString" title="toLocaleDateString() 方法返回该日期对象日期部分的字符串,该字符串格式因不同语言而不同。新增的参数 locales 和 options 使程序能够指定使用哪种语言格式化规则,允许定制该方法的表现(behavior)。在旧版本浏览器中, locales 和 options 参数被忽略,使用的语言环境和返回的字符串格式是各自独立实现的。"><code>Date.prototype.toLocaleDateString</code></a></li>
|
||
<li><a href="Reference/Global_Objects/DateTimeFormat" title="交互示例的源代码存储在 GitHub 资源库。如果你愿意分布交互示例,请复制https://github.com/mdn/interactive-examples,并向我们发送一个pull请求。"><code>Intl.DateTimeFormat</code></a></li>
|
||
</ul>
|
||
</article> |