Date.parse()

  • Revision slug: JavaScript/Reference/Global_Objects/Date/parse
  • Revision title: Date.parse
  • Revision id: 399225
  • Created:
  • Creator: openjck
  • Is current revision? No
  • Comment

Revision Content

Summary

Parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.

Method of Date
Implemented in JavaScript 1.0
ECMAScript Edition ECMAScript 1st Edition

Syntax

Date.parse(dateString)

Parameters

dateString
A string representing an RFC2822 or ISO 8601 date.

Description

The parse method takes a date string (such as "Dec 25, 1995") and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. The local time zone is used to interpret arguments that do not contain time zone information. This function is useful for setting date values based on string values, for example in conjunction with the setTime method and the Date object.

Given a string representing a time, parse returns the time value. It accepts the RFC2822 / IETF date syntax (RFC2822 Section 3.3), e.g. "Mon, 25 Dec 1995 13:30:00 GMT". It understands the continental US time-zone abbreviations, but for general use, use a time-zone offset, for example, "Mon, 25 Dec 1995 13:30:00 +0430" (4 hours, 30 minutes east of the Greenwich meridian). If you do not specify a time zone, the local time zone is assumed. GMT and UTC are considered equivalent.

{{ js_minversion_note("1.8.5", 'A subset of ISO 8601 formatted date strings can now also be parsed.') }}

Alternatively, the date/time string may be in ISO 8601 format. Starting with JavaScript 1.8.5 (Firefox 4), a subset of ISO 8601 is supported. For example, "2011-10-10" (just date) or "2011-10-10T14:48:00" (date and time) can be passed and parsed.

Note that while time zone specifiers are used during date string parsing to properly interpret the argument, they do not affect the value returned, which is always the number of milliseconds between January 1, 1970 00:00:00 UTC and the point in time represented by the argument.

Because parse is a static method of Date, you always use it as Date.parse(), rather than as a method of a Date object you created.

Examples

Example: Using parse

If IPOdate is an existing Date object, then you can set it to August 9, 1995 (local time) as follows:

IPOdate.setTime(Date.parse("Aug 9, 1995"));

Some other examples:

// Returns 807937200000 in time zone GMT-0300, and other values in other
// timezones, since the argument does not specify a time zone.
Date.parse("Aug 9, 1995");
// Returns 807926400000 no matter the local time zone.
Date.parse("Wed, 09 Aug 1995 00:00:00 GMT");
// Returns 807937200000 in timezone GMT-0300, and other values in other
// timezones, since there is no time zone specifier in the argument.
Date.parse("Wed, 09 Aug 1995 00:00:00");
// Returns 0 no matter the local time zone.
Date.parse("Thu, 01 Jan 1970 00:00:00 GMT");
// Returns 14400000 in timezone GMT-0400, and other values in other 
// timezones, since there is no time zone specifier in the argument.
Date.parse("Thu, 01 Jan 1970 00:00:00");
// Returns 14400000 no matter the local time zone.
Date.parse("Thu, 01 Jan 1970 00:00:00 GMT-0400");

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Date" title="JavaScript/Reference/Global_Objects/Date"><code>Date</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.0</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 1st Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code>Date.parse(dateString)</code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
  <dt>
    <code>dateString</code></dt>
  <dd>
    A string representing an <a class="external" href="http://tools.ietf.org/html/rfc2822#page-14" title="http://tools.ietf.org/html/rfc2822#page-14">RFC2822</a> or ISO 8601 date.</dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<p>The <code>parse</code> method takes a date string (such as "<code>Dec 25, 1995</code>") and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. The local time zone is used to interpret arguments that do not contain time zone information. This function is useful for setting date values based on string values, for example in conjunction with the <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Date/setTime" title="JavaScript/Reference/Global_Objects/Date/setTime">setTime</a></code> method and the <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Date" title="JavaScript/Reference/Global_Objects/Date">Date</a></code> object.</p>
<p>Given a string representing a time, <code>parse</code> returns the time value. It accepts the RFC2822 / IETF date syntax (<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>), e.g. "<code>Mon, 25 Dec 1995 13:30:00 GMT</code>". It understands the continental US time-zone abbreviations, but for general use, use a time-zone offset, for example, "<code>Mon, 25 Dec 1995 13:30:00 +0430</code>" (4 hours, 30 minutes east of the Greenwich meridian). If you do not specify a time zone, the local time zone is assumed. GMT and UTC are considered equivalent.</p>
<p>{{ js_minversion_note("1.8.5", 'A <a href="http://www.w3.org/TR/NOTE-datetime">subset of ISO 8601</a> formatted date strings can now also be parsed.') }}</p>
<p>Alternatively, the date/time string may be in <a href="http://www.w3.org/TR/NOTE-datetime" title="http://www.w3.org/TR/NOTE-datetime">ISO 8601</a> format. Starting with JavaScript 1.8.5 (Firefox 4), a <a class="external" href="http://www.w3.org/TR/NOTE-datetime" title="http://www.w3.org/TR/NOTE-datetime">subset of ISO 8601</a> is supported. For example, "<code>2011-10-10</code>" (just date) or "<code>2011-10-10T14:48:00</code>" (date and time) can be passed and parsed.</p>
<p>Note that while time zone specifiers are used during date string parsing to properly interpret the argument, they do not affect the value returned, which is always the number of milliseconds between January 1, 1970 00:00:00 UTC and the point in time represented by the argument.</p>
<p>Because <code>parse</code> is a static method of <code>Date</code>, you always use it as <code>Date.parse()</code>, rather than as a method of a <code>Date</code> object you created.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_parse" name="Example:_Using_parse">Example: Using <code>parse</code></h3>
<p>If <code>IPOdate</code> is an existing <code>Date</code> object, then you can set it to August 9, 1995 (local time) as follows:</p>
<pre class="brush: js">
IPOdate.setTime(Date.parse("Aug 9, 1995"));
</pre>
<p>Some other examples:</p>
<pre class="brush: js">
// Returns 807937200000 in time zone GMT-0300, and other values in other
// timezones, since the argument does not specify a time zone.
Date.parse("Aug 9, 1995");
</pre>
<pre class="brush: js">
// Returns 807926400000 no matter the local time zone.
Date.parse("Wed, 09 Aug 1995 00:00:00 GMT");
</pre>
<pre class="brush: js">
// Returns 807937200000 in timezone GMT-0300, and other values in other
// timezones, since there is no time zone specifier in the argument.
Date.parse("Wed, 09 Aug 1995 00:00:00");
</pre>
<pre class="brush: js">
// Returns 0 no matter the local time zone.
Date.parse("Thu, 01 Jan 1970 00:00:00 GMT");
</pre>
<pre class="brush: js">
// Returns 14400000 in timezone GMT-0400, and other values in other 
// timezones, since there is no time zone specifier in the argument.
Date.parse("Thu, 01 Jan 1970 00:00:00");
</pre>
<pre class="brush: js">
// Returns 14400000 no matter the local time zone.
Date.parse("Thu, 01 Jan 1970 00:00:00 GMT-0400");
</pre>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Date/UTC" title="JavaScript/Reference/Global_Objects/Date/UTC">Date.UTC</a></li>
</ul>
Revert to this revision