diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
| commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
| tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/date/parse | |
| parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
| download | translated-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/date/parse')
| -rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/date/parse/index.html | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/parse/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/parse/index.html new file mode 100644 index 0000000000..701f6fc103 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/parse/index.html @@ -0,0 +1,184 @@ +--- +title: Date.parse() +slug: Web/JavaScript/Reference/Global_Objects/Date/parse +tags: + - Date + - JavaScript + - Method + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Date/parse +--- +<div>{{JSRef("Global_Objects", "Date")}}</div> + +<p><code><strong>Date.parse()</strong></code> 方法解析一个表示某个日期的字符串,并返回从1970-1-1 00:00:00 UTC 到该日期对象(该日期对象的UTC时间)的毫秒数,如果该字符串无法识别,或者一些情况下,包含了不合法的日期数值(如:2015-02-31),则返回值为NaN。</p> + +<p>不推荐在ES5之前使用Date.parse方法,因为字符串的解析完全取决于实现。直到至今,不同宿主在如何解析日期字符串上仍存在许多差异,因此最好还是手动解析日期字符串(在需要适应不同格式时库能起到很大帮助)。 </p> + +<div>{{EmbedInteractiveExample("pages/js/date-parse.html")}}</div> + + + +<h2 id="Syntax" name="Syntax">语法</h2> + +<p>显式调用:</p> + +<pre class="syntaxbox"><code>Date.parse(dateString)</code></pre> + +<p><code>隐式调用:</code></p> + +<pre class="syntaxbox"><code>new Date(<var>dateString</var>).getTime()</code> +</pre> + +<h3 id="Parameters" name="Parameters">参数</h3> + +<dl> + <dt><code>dateString</code></dt> + <dd>一个符合 <a class="external" href="http://tools.ietf.org/html/rfc2822#page-14" title="http://tools.ietf.org/html/rfc2822#page-14">RFC2822</a> 或 ISO 8601 日期格式的字符串(其他格式也许也支持,但结果可能与预期不符)。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<dl> + <dd>一个表示从1970-1-1 00:00:00 UTC到给定日期字符串所表示时间的毫秒数的数值。如果参数不能解析为一个有效的日期,则返回{{jsxref("NaN")}}。</dd> +</dl> + +<h2 id="Description" name="Description">描述</h2> + +<p><code>parse</code> 方法接受一个日期字符串(例如 "<code>Dec 25, 1995</code>"),并返回从1970-1-1 00:00:00 UTC到该日期字符串所表示日期的毫秒数。该方法在基于字符串值设置日期值时很有用,例如结合使用{{jsxref("Global_Objects/Date/setTime", "setTime()")}} 方法和 {{jsxref("Global_Objects/Date", "Date()")}} 构造函数。</p> + +<p><code>parse</code> 方法接受一个表示时间的字符串,返回相应的时间值。该方法可以接受符合 RFC2822 / IETF 日期语法 (<a class="external" href="http://tools.ietf.org/html/rfc2822#page-14" title="http://tools.ietf.org/html/rfc2822#page-14">RFC2822 Section 3.3</a>) 的字符串,如 "<code>Mon, 25 Dec 1995 13:30:00 GMT</code>"。该方法能够理解美国大陆时区的缩写,但是为了更通用,应该使用时区偏移,如 "<code>Mon, 25 Dec 1995 13:30:00 +0430</code>" (格林威治的子午线向东偏移4小时30分钟)。如果没有指定时区,默认使用本地时区。</p> + +<p>GMT 和 UTC 被看作相等。 如果 <a class="external" href="http://tools.ietf.org/html/rfc2822#page-14" title="http://tools.ietf.org/html/rfc2822#page-14">RFC2822 Section 3.3</a> 格式中不包含时区信息时,会以本地时区来解析日期字符串。</p> + +<p>由于在解析日期字符串时存在偏差会导致结果不一致,因此推荐始终手动解析日期字符串,特别是不同的ECMAScript实现会把诸如“2015-10-12 12:00:00”的字符串解析为NaN,UTC或者本地时间。</p> + +<h3 id="ECMAScript_5_ISO-8601_日期格式支持">ECMAScript 5 <span class="caps">ISO</span>-8601 日期格式支持</h3> + +<p>另外,日期时间字符串也可以使用 <a href="http://www.w3.org/TR/NOTE-datetime" title="http://www.w3.org/TR/NOTE-datetime">ISO 8601</a> 格式。例如,"<code>2011-10-10</code>" (仅日期)或 "<code>2011-10-10T14:48:00</code>" (日期和时间)能够作为参数被传递和解析。 如果参数字符串只包含日期格式,那么将会使用UTC时区来解析该参数。而如果是<a href="http://www.w3.org/TR/NOTE-datetime" title="http://www.w3.org/TR/NOTE-datetime">ISO 8601</a> 格式中规定的时间加日期的格式,则将会被作为本地时区处理。</p> + +<p>虽然在日期字符串解析过程中会使用时区修饰符,但返回值总会是从由NaN表示的1970-1-1 00:00:00 UTC到该日期字符串所表示日期的毫秒数。</p> + +<p>由于 <code>parse()</code> 是 {{jsxref("Date")}} 的一个静态方法 , 所以应该使用 <code>Date.parse()</code> 来调用,而不是作为 {{jsxref("Date")}} 的实例方法。</p> + +<h3 id="Differences in assumed time-zone" name="Differences in assumed time-zone">默认时区的区别</h3> + +<p>当输入为 "<code>March 7, 2014</code>" 时, <code>parse()</code> 将默认使用本地时区。但如果使用 ISO 格式如 <code>"2014-03-07"</code> ,则会被默认为 UTC (ES5 和 ECMAScript 2015) 时区。 因此除非系统本地时区为 UTC,由这些字符串解析出的 {{jsxref("Date")}} 对象可能会因为 ECMAScript 版本不同而代表不同的时间。这意味着两个看起来等效的字符串可能因为它们的格式不同而被转换成不同的值。</p> + +<h3 id="引擎相关的日期格式">引擎相关的日期格式</h3> + +<p>ECMAScript 规范规定:如果一个字符串不符合标准格式,则函数可以使用任何由引擎决定的策略或解析算法。 <code>Date.parse()</code> 对于因包含有无效元素而无法识别的 ISO 格式字符串或者日期应该返回 {{jsxref("NaN")}} 。</p> + +<p>但是, 在如 ECMA-262 规范中定义的情况,如果因为无效值而导致日期字符串不能被识别为 ISO 格式时,根据浏览器和给定的值不同,返回值可以是,也可以不是 {{jsxref("NaN")}} 。比如:</p> + +<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="comment token">// 包含无效值的非 ISO 格式字符串</span> +<span class="keyword token">new</span> <span class="class-name token">Date</span><span class="punctuation token">(</span><span class="string token">'23/25/2014'</span><span class="punctuation token">)</span><span class="punctuation token">;</span></code></pre> + +<p>在 Firefox 30 中会被识别为本地时区的2015年12月25日,而在 Safari 7 中则是无效日期。但是,如果字符串被识别为 ISO 格式并且包含无效值,则在所有遵循 ES5 或者更新标准的浏览器中都会返回 {{jsxref("NaN")}} 。</p> + +<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="comment token">// 包含无效值的 ISO 格式字符串</span> +<span class="keyword token">new</span> <span class="class-name token">Date</span><span class="punctuation token">(</span><span class="string token">'2014-25-23'</span><span class="punctuation token">)</span><span class="punctuation token">.</span><span class="function token">toISOString</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span> +<span class="comment token">// 在所有遵循 ES5的浏览器中返回 "RangeError: invalid date"</span></code></pre> + +<p>SpiderMonkey 的引擎策略可以在 <a href="http://mxr.mozilla.org/mozilla-central/source/js/src/jsdate.cpp?rev=64553c483cd1#889"><code>jsdate.cpp</code></a> 中找到。字符串 <code>"10 06 2014"</code> 可以作为非 ISO 格式字符串使用自定义处理方式的例子。参见这篇关于解析如何进行的<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1023155#c6">粗略纲要</a>。</p> + +<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="keyword token">new</span> <span class="class-name token">Date</span><span class="punctuation token">(</span><span class="string token">'10 06 2014'</span><span class="punctuation token">)</span><span class="punctuation token">;</span></code></pre> + +<p>将会被解析为本地时间 2014年10月6日,而不是6月10日。另一个例子</p> + +<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="keyword token">new</span> <span class="class-name token">Date</span><span class="punctuation token">(</span><span class="string token">'foo-bar 2014'</span><span class="punctuation token">)</span><span class="punctuation token">.</span><span class="function token">toString</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span> +<span class="comment token">// 返回: "Invalid Date"</span> + +Date<span class="punctuation token">.</span><span class="function token">parse</span><span class="punctuation token">(</span><span class="string token">'foo-bar 2014'</span><span class="punctuation token">)</span><span class="punctuation token">;</span> +<span class="comment token">// 返回: NaN</span></code></pre> + +<h2 id="Examples" name="Examples">例子</h2> + +<h3 id="Example:_Using_parse" name="Example:_Using_parse">例子:使用 <code>Date.parse()</code></h3> + +<p>如果 <code>IPOdate</code> 是一个已经存在的 {{jsxref("Date")}} 对象,则可以把其设置为本地时间 1995年8月9日。如下:</p> + +<pre class="brush: js line-numbers language-js"><code class="language-js">IPOdate<span class="punctuation token">.</span><span class="function token">setTime</span><span class="punctuation token">(</span>Date<span class="punctuation token">.</span><span class="function token">parse</span><span class="punctuation token">(</span><span class="string token">'Aug 9, 1995'</span><span class="punctuation token">)</span><span class="punctuation token">)</span><span class="punctuation token">;</span></code></pre> + +<p>其他一些解析非标准格式日期的例子:</p> + +<pre class="brush: js">Date.parse("Aug 9, 1995"); +</pre> + +<p>在时区 GMT-0300 中返回 807937200000 ,在其他时区中返回另外的值,因为未指定时区并且不是 ISO 格式,所以默认使用本地时区。</p> + +<pre class="brush: js">Date.parse("Wed, 09 Aug 1995 00:00:00 GMT"); +</pre> + +<p>因为指定了时区 GMT (UTC),所以不管本地时区如何,总是返回 <code>807926400000</code> 。</p> + +<pre class="brush: js">Date.parse("Wed, 09 Aug 1995 00:00:00"); +</pre> + +<p>在时区 GMT-0300 中返回 807937200000 ,在其他时区中返回另外的值,因为没有时区标志并且不是 ISO 格式,所以作为本地时区处理。</p> + +<pre class="brush: js">Date.parse("Thu, 01 Jan 1970 00:00:00 GMT"); +</pre> + +<p>因为指定了时区 GMT (UTC),所以不管本地时区如何,总是返回<code> 0</code> 。</p> + +<pre class="brush: js">Date.parse("Thu, 01 Jan 1970 00:00:00"); +</pre> + +<p>在时区 GMT-0400 中返回 <code>14400000</code>,在其他时区中返回另外的值,因为未指定时区并且不是 ISO 格式,所以使用本地时区处理。</p> + +<pre class="brush: js">Date.parse("Thu, 01 Jan 1970 00:00:00 GMT-0400"); +</pre> + +<p>因为指定了时区 GMT (UTC),所以不管本地时区如何,总是返回<code> </code><code>14400000</code> 。</p> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范版本</th> + <th scope="col">规范状态</th> + <th scope="col">注解</th> + </tr> + <tr> + <td>ECMAScript 1st Edition. Implemented in JavaScript 1.0</td> + <td>Standard</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.9.4.2', 'Date.parse')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td>ISO 8601 format added</td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-date.parse', 'Date.parse')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-date.parse', 'Date.parse')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p> </p> + +<div class="hidden"> +<p>The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a>and send us a pull request.</p> +</div> + +<p>{{Compat("javascript.builtins.Date.parse")}}</p> + +<h2 id="See_also" name="See_also">兼容性提示</h2> + +<p>Firefox 49 {{geckoRelease(49)}} 修改了解析2位数年份的方式,从和 Internet Explorer 一致改为和 Google Chrome 浏览器一致。现在,2位数的年份小于等于 <code>50</code> 的将会被解析为21世纪的年份。比如, <code>04/16/17</code> ,在之前会被解析为 1917年4月16日,现在将被解析为 2017年4月16日。为了避免任何可能的同步问题或者有歧义的年份,推荐使用 ISO 8601 格式如 "2017-04-16" ({{bug(1265136)}})。</p> + +<h2 id="See_also" name="See_also">相关链接</h2> + +<ul> + <li>{{jsxref("Date.UTC()")}}</li> +</ul> |
