To convert a String to a Date in JavaScript, you can use the “Date” constructor and parse a variety of date formats, including ISO 8601 date strings and strings in the format “Month day, year”.
Example
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
In this code, we have a date string called dateString.
We created a Date object called dateObject by passing the date string to the Date constructor. The Date object will represent the date specified in the string.
Remember that the Date constructor may not be able to parse all date string formats, and the parsing behavior can be browser-dependent.
For more reliable date string parsing, consider using a library like date-fns or Moment.js (note that Moment.js is no longer actively maintained and not recommended for new projects).

Niva Shah is a Software Engineer with over eight years of experience. She has developed a strong foundation in computer science principles and a passion for problem-solving.