We can easily convert a string to a date object using the function Date(). It accepts the string in different formats.

For example:

We can pass the year, month, and day separately such that it automatically creates the date object for us.

// new Date (yyyy, mm, dd)
var date = new Date(2022, 01, 23);

We can also pass the date string to a Date function in ISO format such that the Date object can be created.

// new Date(yyyy/mm/dd);
var date = new Date(2022/01/30);

//new Date(yyyy-mm-dd);
var date = new Date(2022-01-23);

Categorized in:

Tagged in: