How to Convert a String to a Date object in JavaScript

Convert a String to a Date object in JavaScript

To Convert a String to a Date object in JavaScript

  1. Create a new Date object with the Date() constructor.
  2. 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);
Convert a String to a Date object in JavaScript
Convert a String to a Date object in JavaScript.

Here’s what the above code is doing:

  1. We used the Date() constructor to create a new Date object.
  2. We passed the string ‘2022-10-06’ to the Date() constructor.
  3. 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.

  1. A valid ISO 8601 string is either formatted as YYYY-MM-DDTHH:mm:ss.sssZ, or just YYYY-MM-DD if you only have a date without time.
  2. Comma-separated arguments that represent the year, month (0 = January to 11 = December), day of the month, hours, minutes and seconds.

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);
Splits an MMDDYYYY formatted string
Splits an MMDDYYYY formatted string

Here’s what the above code is doing:

  1. We have a string that represents a date in the format YYYY/MM/DD.
  2. We use the split() method to split the string into an array of strings.
  3. We split the date into its month, day, and year values by splitting it at each forward slash.
  4. We use the destructuring assignment to assign the first, second, and third elements of the array to the variables month, day, and year.
  5. We use the unary plus operator to convert the strings to numbers.
  6. 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);
Creates a Date that contains the hours, minutes and seconds.
Creates a Date that contains the hours, minutes and seconds.

Here’s what the above code is doing:

  1. We split the date string into two parts: the date values and the time values.
  2. We split the date values into three parts: the month, the day, and the year.
  3. We split the time values into three parts: the hours, the minutes, and the seconds.
  4. 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());
Get the ISO formatted date by calling the toISOString() method.
Get the ISO formatted date by calling the toISOString() method.

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.