In JavaScript, converting a string to a date is a common task when working with date values. Whether you’re parsing user input, manipulating dates, or storing them in a database, it’s crucial to understand the various methods available for converting strings to dates. In this article, we will cover the techniques to convert a String to Date Object in JavaScript. Let us get started!
What are Strings & Date Objects?
As we know, Strings represent a sequence of multiple characters. ‘C’ represents a single character and “Cat” represents a string. In JavaScript, we can declare a string in two ways.
One on the other side, the Date object represents a point in time in JavaScript. It can be used to work with dates and times. In further sections, we will see how to create Date Objects from Date Strings.
The ISO 8601 calendar date extended format is the standard format maintained by the ISO This standard provides a well-defined and standard method of representing calendar dates and times in worldwide communications.
According to the 8601 Calendar Format, the Date is represented as yyyy-mm-dd.
3 Methods To Convert a String to Date in JavaScript
Dates are a fundamental concept in programming, and conversion to date from string is an essential operation. In this section, we will discuss various methods to convert a string to date in JavaScript. Let us get started!
1) Using Date() Constructor
The Date() is the constructor of the Date class. It allows us to create new date objects by taking a string as a parameter. The string passed as the parameter is converted into the date object given that the format of the string should be in the ISO 8601 standard format (yyyy-mm-dd). This sums up a supported and reliable conversion.
The syntax of the same is as follows:
let dateObject = Date(dateString);
where,
dateObject: variable to store the date object returned from the Date() function.
dateString: variable representing the string to be converted to the date object
Let us take an example:
//declaring a Date String let strDate='2023-11-17'; //printing the value and type of strDate console.log(strDate + " " + typeof(strDate)); //using the Date() constructor let dateObj=new Date(strDate); //printing the type of dateObj console.log(dateObj + " " + typeof(dateObj));
Output:
2023-11-17 string
Fri Nov 17 2023 05:30:00 GMT+0530 (India Standard Time) object
On observing the output carefully, we conclude that the Date String in the 8601 format has been converted to the Date object.
Let us try to form a date object with some other date format (other than 8601).
//declaring a Date String let strDate='17-11-2023'; //printing the value and type of strDate console.log(strDate + " " + typeof(strDate)); //using the Date() constructor let dateObj=new Date(strDate); //printing the type of dateObj console.log(dateObj + " " + typeof(dateObj));
Output:
17-11-2023 string
Invalid Date object
So, the output shows an invalid date object specifying that the acceptable format for the date string to be passed as a parameter is “yyyy-mm-dd”. On the other note, the output also shows an invalid date object when we pass an invalid month or invalid day.
2) Using Date.Parse() Method
Another easy and straightforward method to convert string to date in JavaScript is by using the Date.parse() method. This method parses a date string. It returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
This numerical value is received from the Date.parse() method can be useful for various operations, such as date comparisons or calculations.
The syntax of the same is as follows.
let milliseconds= Date.parse(dateString);
where,
milliseconds: variable to store the milliseconds since January 1, 1970.
dateString: variable representing the string to be converted to no of milliseconds, can be of the format “year-month-date” or “year/month/date”
Here’s an example:
//declaring a Date String let strDate='2023/11/17'; //printing the value and type of strDate console.log(strDate + " " + typeof(strDate)); //using the Date.parse() method let millisecs=Date.parse(strDate); //printing the milliseconds console.log(millisecs);
Output:
17 Nov 2023 string
1700159400000
Such a long numerical value represents the number of milliseconds that have elapsed since January 1, 1970. We have obtained the desired output.
3) Using the Date() Constructor with Different Parameters
Both of the functions discussed above accept the string input in the 8061 extended format. If the date is not in the standard format, we use the below method to convert the string date to the date object.
The Date class has another Date() constructor that takes parameterized input. The syntax of the same is given below:
let dateObj = new Date(year, monthIndex, day, hours, minutes, seconds);
where,
dateObj: variable to store the date object returned from the Date() function
Year: all 4 digits of the year should be entered
monthIndex: index of the months starting from 0, Jan-0, Feb-1, Mar-2, and so on.
Day: enter the day value as it is (from 1 to 31)
hours: pass the value of hours in Railway time, eg. 1:00 pm, 2:00 pm, 3:00 pm will be taken as 13, 14, 15.
minutes: varies from 0 to 60
seconds: varies from 0 to 60
Here’s an example of the same:
//using the Date(year, monthInd, day, hours, minutes, seconds) method let dateObj = new Date(2023, 2, 17, 8, 14, 20); //printing the dateObj console.log(dateObj);
Output:
2023-03-17T02:44:20.000Z
We have obtained the desired output.
We can also convert strings to array using various methods.
Conclusion
Converting a string to date in JavaScript is an essential skill for working with date values effectively. In this article, we explained different ways to do this conversion using Date() constructor and Date.Parse() method.