How to Convert String to Date in JavaScript [3 Ways]

Here are three ways to convert String to Date in JavaScript:

  1. Using Date() constructor
  2. Using Date.parse()
  3. Using toDateString()

Method 1: Using Date() Constructor

The Date constructor parses the string and returns a Date object representing the given date and time string. If the string is recognized as valid, it parses it; otherwise, it returns an “Invalid Date”.

const dateString = '2023-04-22';

// Convert the string to a Date object
const dateObject = new Date(dateString);

// Log the Date object to the console
console.log(dateObject);

Output

2023-04-22T00:00:00.000Z

Method 2: Using Date.parse()

The Date.parse() is a static method “used to parse a string representation of a date and returns the date’s timestamp”.

let date = Date.parse("2023-6-05 GMT");
console.log(date);

let date2 = Date.parse("1970/1/1 GMT");
console.log(date2);

// invalid format
let date3 = Date.parse("19-05-2022");
console.log(date3);

Output

1685923200000
0
NaN

Method 3: Using toDateString()

The toDateString() method of Date instances returns a string representing the date portion of this date interpreted in the local timezone.

const dt = new Date(0);

console.log(dt.toString());

Output

Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time)

That’s it.

Related posts

JavaScript Date to String

JavaScript Date toUTCString()

JavaScript Date toTimeString()