Explain Codes LogoExplain Codes Logo

How to create a date object from string in javascript

javascript
date-parsing
date-object
javascript-date
Nikita BarsukovbyNikita Barsukov·Feb 16, 2025
TLDR

Here's a quick way to convert a string into a Date object:

let dateObject = new Date('2023-03-21');

This simple method works best when the input string is in ISO 8601 format (YYYY-MM-DD or YYYY-MM-DDTHH:mm:ss.sssZ). The method is concise, consistent, and cross-browser friendly.

If you're dealing with non-standard date formats, here's a manual option for conversion:

let dateString = "30/11/2011"; let parts = dateString.split("/"); // remember, months in JS are like arrays, they start from 0! let dateObject = new Date(parts[2], parts[1] - 1, parts[0]);

Breakdown of date parsing

Parsing strings to create Date objects can be a tricky journey due to variations across browser implementations. Even though the new Date() constructor accepts several date string formats, the safest way to keep your sanity is to stick to the ISO 8601 standard. If you're dealing with non-standard formats, equip yourself with custom parsing logic.

Helping JavaScript count months right

In the world of JavaScript, January started having an identity crisis and began calling itself 0. Hence, the concept of zero-indexed months where January is 0 and December is 11.

let dateString = "30/11/2011"; let [day, month, year] = dateString.split('/').map(part => parseInt(part, 10)); // JS counts months from 0, so consider it while creating date. let dateObject = new Date(year, month - 1, day);

Decoding dates with Date.parse()

JavaScript also offers a sneakier method using Date.parse(), where we get a timestamp from a well-mannered date string:

let timestamp = Date.parse('2011-11-30T00:00:00.000Z'); let dateObject = new Date(timestamp);

Smooth wrapping with a bow tie

Keeping it classy, you can use the new Date() syntax with the timestamp as a single argument:

let dateObject = new Date(Date.parse('2011-11-30T00:00:00.000Z'));

A JVM - JavaScript visual metaphor

Imagine setting up a time-travel device by entering the required date:

Format: "MM-DD-YYYY" or "YYYY-MM-DD" or any ISO 8601 format

And voila! You've created your Date object:

const timeTravelDevice = new Date('2021-12-17');

Power-up time:

Input: "2021-12-17" (an innocent string) Output: Time travel device powered up (a robust Date object!)

Special attention scenarios

Different timezone, different groove

When dealing with timezone in ISO strings, use Z or +/-HH:MM. A missing timezone suffix means the string will be considered as local time. If your string is partying in UTC, ensure it's dressed appropriately with a Z on the end:

let dateString = '2011-11-30T00:00:00Z'; // Hey, I am in UTC! var utcDate = new Date(dateString);

366, a leap year's reality show

A shoutout to those leap years - adding that extra day - February 29, can seem miraculous except for those feeling one year older. For the code, it's just another day:

var leapDate = new Date(2020, 1, 29); // Works, because, surprise!, 2020 is a leap year!

Parsing Delilah, the tricky date formats

Date formats come in all shapes and sizes. One day you could be sipping tea with DD/MM/YYYY format and the very next day be dining with the YYYY-MM-DD format. Utilities like moment.js and date-fns help parse and manipulate these diverse date formats:

let moment = require('moment'); let dateObject = moment('30/11/2011', 'DD/MM/YYYY').toDate();