The method Date.parse(str) can create a date from a string.
The call to Date.parse(str) parses the string in the given format.
It returns the timestamp as number of milliseconds from 1 Jan 1970 UTC+0.
If the format is invalid, returns NaN
.
Direct call:
Date.parse(dateString)
Implicit call:
new Date(dateString)
dateString
- A string representing a simplification of the ISO 8601 calendar date extended format. Other formats may be used, but results are implementation-dependent.A number representing the milliseconds elapsed since January 1, 1970, 00:00:00 UTC.
It is the date obtained by parsing the given string representation of a date.
If the argument doesn't represent a valid date, NaN is returned.
The string format should be: YYYY-MM-DDTHH:mm:ss.sssZ
, where:
Value | Meaning |
---|---|
YYYY-MM-DD | the date: year-month-day. |
"T" | the delimiter. |
HH:mm:ss.sss | the time: hours, minutes, seconds and milliseconds. |
optional 'Z' | denotes the time zone in the format +-hh:mm. A single letter Z that would mean UTC+0. |
Date.parse(str) supports the shorter variants: YYYY-MM-DD or YYYY-MM or YYYY.
For instance:
let ms = Date.parse('2020-02-24T15:54:50.123-07:00'); console.log(ms);
We can create a new Date object from the timestamp:
let date = new Date( Date.parse('2020-01-24T13:45:50.123-07:00') ); console.log(date);
More example
var d = Date.parse("March 21, 2012"); console.log(d);