Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:Date:parse

From MDC


Contents

[edit] 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
Static
Implemented in: JavaScript 1.0, NEX 2.0
ECMA Version: ECMA-262

[edit] Syntax

Date.parse(dateString)

[edit] Parameters

dateString 
A string representing a date.

[edit] 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 IETF standard (RFC 1123 Section 5.2.14 and elsewhere) date syntax: "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 GMT+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.

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.

[edit] Examples

[edit] 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");

[edit] See also

Date.UTC