Convert a String to a Date object in JavaScript
To Convert a String to a Date object in JavaScript
- Create a new Date object with the
Date()
constructor. - Pass a valid date string as a parameter to the a Date object.
Example –
const theDateString = '2022-10-06';
const date = new Date(theDateString);
console.log(date);
Here’s what the above code is doing:
- We used the
Date()
constructor to create a new Date object. - We passed the string ‘2022-10-06’ to the
Date()
constructor. - The
Date()
constructor parsed the string and created a Date object.
If the Date object is not valid, the string must be formatted correctly before being passed to the Date()
constructor.
You can pass two different types of arguments to the Date()
constructor if you’re having trouble creating a valid Date object.
- A valid ISO 8601 string is either formatted as
YYYY-MM-DDTHH:mm:ss.sssZ
, or justYYYY-MM-DD
if you only have a date without time. - Comma-separated arguments that represent the
year
,month
(0 = January to 11 = December),day of the month
,hours
,minutes
andseconds
.
This is an example of how to take a string formatted as MM/DD/YYYY
(or any other format) and use the Date()
constructor to create a Date object from it.
Example –
const theDateString = '2022/10/06';
const [month, day, year] = theDateString.split('/');
console.log(month);
console.log(day);
console.log(year);
const date = new Date(+year, +month - 1, +day);
console.log(date);
Here’s what the above code is doing:
- We have a string that represents a date in the format
YYYY/MM/DD
. - We use the split() method to split the string into an array of strings.
- We split the date into its month, day, and year values by splitting it at each forward slash.
- We use the destructuring assignment to assign the first, second, and third elements of the array to the variables month, day, and year.
- We use the unary plus operator to convert the strings to numbers.
- We use the Date constructor to create a new Date object.
The Date constructor expects the year, month, and day as parameters.
The month is 0-based, which means that January = 0 and December = 11.
The unary plus operator converts the string to a number.
The Date()
object is always created in the local time zone.
Create a Date that contains the hours, minutes, and seconds.
Example –
const theDateString = '2022/10/06 07:30:14';
const [dateValues, timeValues] = theDateString.split(' ');
console.log(dateValues);
console.log(timeValues);
const [month, day, year] = dateValues.split('/');
const [hours, minutes, seconds] = timeValues.split(':');
const date = new Date(+year, +month - 1, +day, +hours, +minutes, +seconds);
console.log(date);
Here’s what the above code is doing:
- We split the date string into two parts: the date values and the time values.
- We split the date values into three parts: the month, the day, and the year.
- We split the time values into three parts: the hours, the minutes, and the seconds.
- We create a new
Date()
object using the year, month, day, hours, minutes, and seconds.
The approach is the same if your separator is different, e.g. a hyphen.
Note – It is recommended that you store date strings in your database using the ISO 8601 standard.
To get an ISO formatted date, call the toISOString() method.
Example –
const theDateString = '2022/10/06 07:30:14';
const [dateValues, timeValues] = theDateString.split(' ');
console.log(dateValues);
console.log(timeValues);
const [month, day, year] = dateValues.split('/');
const [hours, minutes, seconds] = timeValues.split(':');
const date = new Date(+year, +month - 1, +day, +hours, +minutes, +seconds);
console.log(date);
console.log(date.toISOString());
The toISOString()
method returns a string in the ISO 8601 format representing the given date. The ISO 8601 format is a standardized format for representing dates and times.
The timezone is always zero UTC offset, as denoted by the ‘Z’ suffix.