Ways To Convert A String To A Date Object In JavaScript

The Date is an essential piece of information, and there are many ways to convert a String to a Date object in JavaScript. In this article, I will show some methods to do it, for example, using the constructor of Date class ; Date.parse(); using a third-party library; or splitting the string and converting it into a Date object.

Convert A String To A Date Object In JavaScript

Using new Date() Function

We can use a built-in JavaScript class called Date class. Use the constructor of this class to convert a String to a Date object. The parameter should be a string representing a date.

Syntax:

new Date(dateString);

This method accepts one parameter. In this case, the parameter should be a date string.

Example:

// Pass string into the constructor of Date class
const dateObject = new Date("2022/03/30 GMT");
console.log(dateObject);

Output:

Wed Mar 30 2022 07:00:00

Using Date.parse() function

You can use this method of the Date class in the same way you would use the Date class’s constructor. It returns a number that is the number of milliseconds. Then pass it into the constructor of the date class to get the Date object.

Syntax:

Date.parse(dateString);

This method will return the time in milliseconds. The parameter must be a date string.

Example:

// Use parse() method to convert String to time in milliseconds
const milliseconds = Date.parse("2022/03/30 GMT");

// Pass the time in milliseconds into the constructor of Date class
const dateObj = new Date(milliseconds);
console.log(dateObj);

Output:

Wed Mar 30 2022 07:00:00

Split the string and convert it into Date format

If you don’t want to use the above two methods, you can extract the date parts from the date string and pass them all into the Date class constructor. We will use the split() method to split the day, month, and year in the string, then we will put the day, month, and year into the Date class constructor and get the Date object.

Example:

const strDay = "03/30/2022";

// Use the split() method to convert string to Date object
const [month, day, year] = strDay.split("/");

const dateObject = new Date(+year, +month - 1, +day);
console.log(dateObject);

Output:

Wed Mar 30 2022 00:00:00

Use a third-party library like moment.js

Using the moment.js library to convert a date string into a date object is a great method to convert a String object to a Date object in JavaScript. Look at the following example to understand more. First, import the moment.js library, then pass the time string and date format into the moment() function. Moment() function will return an object, and call toDate() method on that object to get the Date object.

Example:

import moment from "moment";

// Use moment() function of moment.js library
const mDate = moment("03-30-2022", "MM-DD-YYYY");
const dateObject = mDate.toDate();
console.log(dateObject);

Output:

Wed Mar 30 2022 00:00:00

Summary

We can use the constructor or parse() method in the Date class to convert a string to a JavaScript date object. No matter which way you use, the parameter should be a string representing a date, or you can use the split method to extract the date parts from the date string and pass them all into the Date class constructor. The split() method is highly recommended because it is easy to use, convenient and suitable for different types of strings, but you can use whichever method is suitable for your work.

Leave a Reply

Your email address will not be published. Required fields are marked *