Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Date
  6. parse()

Date.parse()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

TheDate.parse() static method parses a string representation of a date, and returns the date'stimestamp.

Try it

// Standard date-time string formatconst unixTimeZero = Date.parse("1970-01-01T00:00:00Z");// Non-standard format resembling toUTCString()const javaScriptRelease = Date.parse("04 Dec 1995 00:12:00 GMT");console.log(unixTimeZero);// Expected output: 0console.log(javaScriptRelease);// Expected output: 818035920000

Syntax

js
Date.parse(dateString)

Parameters

dateString

A string inthe date time string format. See the linked reference for caveats on using different formats.

Return value

A number representing thetimestamp of the given date. IfdateString fails to be parsed as a valid date,NaN is returned.

Description

This function is useful for setting date values based on string values, for example in conjunction with thesetTime() method.

The formats thatparse() can handle are not explicitly specified, but there are a fewinvariants:

  • Thedate time string format (produced bytoISOString()) must be supported.
  • Ifx is any Date whose milliseconds amount is zero, thenx.valueOf() should be equal to any of the following:Date.parse(x.toString()),Date.parse(x.toUTCString()),Date.parse(x.toISOString()). This means the formats produced bytoString() andtoUTCString() should be supported too.
  • The spec doesnot require support for the format produced bytoLocaleString(). However, major engines all try to supporttoLocaleString("en-US") format.

Other formats are implementation-defined and may not work across all browsers. A library can help if many different formats are to be accommodated. In fact, the unreliability ofDate.parse() is one of the motivations for theTemporal API to be introduced.

Becauseparse() is a static method ofDate, you always use it asDate.parse(), rather than as a method of aDate object you created.

Examples

Using Date.parse()

The following calls all return1546300800000. The first will imply UTC time because it's date-only, and the others explicitly specify the UTC timezone.

js
Date.parse("2019-01-01");Date.parse("2019-01-01T00:00:00.000Z");Date.parse("2019-01-01T00:00:00.000+00:00");

The following call, which does not specify a time zone will be set to 2019-01-01 at 00:00:00 in the local timezone of the system, because it has both date and time.

js
Date.parse("2019-01-01T00:00:00");

toString() and toUTCString() formats

Apart from the standard date time string format, thetoString() andtoUTCString() formats are supported:

js
// toString() formatDate.parse("Thu Jan 01 1970 00:00:00 GMT-0500 (Eastern Standard Time)");// 18000000 in all implementations in all timezones// toUTCString() formatDate.parse("Thu, 01 Jan 1970 00:00:00 GMT");// 0 in all implementations in all timezones

Non-standard date strings

Note:This section contains implementation-specific behavior that may be inconsistent across browsers or different versions of browsers. It is not meant to be a comprehensive browser compatibility table and you should always conduct your own tests before using any format in your code.

Implementations usually default to the local time zone when the date string is non-standard. For consistency, we will assume that the runtime uses the UTC timezone, and unless specified otherwise, the output will vary with the device's time zone.Daylight Saving Time (DST), of the local time zone, can also have an effect on this.

Here are some more examples of non-standard date strings. Browsers are very lenient when parsing date strings and may discard any part of a string that they cannot parse. For compatibility reasons, browsers often copy each other's behavior, so these handling patterns tend to propagate cross-browser. As previously stated, the following examples are for illustration only, and are not exhaustive by any means:

DescriptionExampleChromeFirefoxSafari
Single number0 (single-digit)946684800000 (Jan 01 2000); NaN in Firefox ≤122-62167219200000 (Jan 01 0000)
31 (two-digit)NaN-61188912000000 (Jan 01 0031)
999 (three-/four-digit)-30641733102000 (Jan 01 0999)
Date strings that use different separators1970-01-01 (standard)0 in all time zones
1970/01/010
1970,01,010NaN
1970 01 010NaN
Strings that look liketoString()Thu Jan 01 1970 00:00:00
Thu Jan 01 1970
Jan 01 1970 00:00:00
Jan 01 1970
0
Strings that look liketoUTCString()Thu, 01 Jan 1970 00:00:00
Thu, 01 Jan 1970
01 Jan 1970 00:00:00
01 Jan 1970
0
First date component is 2-digit01-02-03 (first segment can be valid month)1041465600000 (Jan 02 2003)-62132745600000 (Feb 03 0001)
Note: Safari always assumes YY-MM-DD, but MM/DD/YY.
27-02-03 (first segment can be valid day but not month)NaN-61312291200000 (Feb 03 0027)
49-02-03 (first segment cannot be valid day and is <50)2495923200000 (Feb 03 2049)-60617980800000 (Feb 03 0049)
50-02-03 (first segment cannot be valid day and is ≥50)-628300800000 (Feb 03 1950)-60586444800000 (Feb 03 0050)
Out-of-bounds date components2014-25-23
Mar 32, 2014
2014/25/23
NaN
2014-02-301393718400000 (Mar 02 2014)NaN
02/30/20141393718400000
Extraneous characters after the month name04 Dec 1995
04 Decem 1995
04 December 1995
818031600000
04 DecFoo 1995818031600000
Only the first three characters are read.
Firefox ≤121 reads up to the valid month name, thus returning NaN when it sees "F".
04 De 1995NaN

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-date.parse

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp