Skip to content
Home » How to Convert a String Into Date in JavaScript

How to Convert a String Into Date in JavaScript

In this tutorial let’s learn about How to Convert a String Into Date in JavaScript. It is sometimes necessary to transform a string into a date format. Date-specific operations are not allowed because the data is in string format. As a result, we must transform the string to a date object. Here are a couple methods for converting a string to a date.

Convert a String Into Date using new Date() Function in JavaScript

The new Date() function is the most often used method for converting a string to a date. create Date() accepts parameters in the following formats but returns a date object.

  1. As an argument, there are no parameters. If no parameters are supplied to the new Date() function, it will return the current system date and time, including the time-zone information in the local system time.
  2. The argument is a date object. The new Date() function will return a date object that corresponds to the date text supplied as an input.
  3. A string date is passed. The most intriguing aspect of the new Date(‘2022-4-5’) function is that it can convert a date supplied as an input in string format into a date format.

When we send a string date to the new Date() function, it is turned into a date object. The string format should be YYYY-MM-DD.

Code Example :

let d = '2022-04-15';
console.log(new Date(d));

Output :

2022-04-15T00:00:00.000Z

Code Example :

let d = 'June 3,2022 1:30:00';
console.log(new Date(d));

Output :

2022-06-02T20:00:00.000Z

Code Example :

let d = '18-05-2022';
console.log(new Date(d));

Output :

Invalid Date

The new Date() method may handle strings in the YYYY-MM-DD or YYYY/MM/DD format. If we use a different string format that does not comply with ISO 8601 standards, the new Date() function will fail to parse the date and return an Invalid date.

Convert a String Into Date using Date parse() Function in JavaScript

Date.parse() is another method for converting a string date. Instead of a date object, it returns a numeric number. As a result, if you expect a date object, it will necessitate further processing. It turns the parsed date to an integer representing the milliseconds since midnight on January 1, 1970. It is similar to the timestamp format, but instead of seconds, Date.parse() returns a milliseconds value.

Code Example :

let str = 'June 15, 2022';
d = Date.parse(str);
console.log(d);

Output :

1655231400000

Code Example :

let str = '2022-06-15';
d = Date.parse(str);
console.log(d);

Output :

1655231400000

Conclusion

If the string belongs to the ISO 8601 standard, the most often used new Date() function is quite handy for converting a textual date into a date object. The Date.parse() function varies from the new Date() function in that it returns a numeric value including the milliseconds since January 1st, 1970 that have elapsed till the date supplied as a string parameter.

Similar Posts:

How to Convert String to Float in Python
Get the Last Character of a String in JavaScript
Convert Text File to PDF using Node.js Express jsPDF