Javascript - Date from String

Introduction

Date.parse() method accepts a string argument representing a date.

It attempts to convert the string into a millisecond representation of a date.

The following format are supported

Format Example
month/date/year 6/13/2018
month_name date, year January 12, 2018
day_of_week month_name date year hours:minutes:seconds time_zone Tue May 25 2018 00:00:00 GMT-0700
ISO 8601 extended format YYYY-MM-DDTHH:mm:ss.sssZ 2018-05-25T00:00:00).

To create a date object for May 25, 2018, you can use the following code:

var someDate = new Date(Date.parse("May 25, 2018"));

If the string passed into Date.parse() doesn't represent a date, then it returns NaN.

The Date constructor will call Date.parse() behind the scenes if a string is passed in.

The following code is identical to the previous example:

var someDate = new Date("May 25, 2018");

Related Topic