Anjan Dutta

Convert any date string to date object in Javascript

Convert any date string to date object in Javascript

Updated On: 09/01/2024

One common task in JavaScript is converting date strings into date objects. This becomes crucial when dealing with APIs that often send dates as strings. Let's dive into the world of converting strings to dates in JavaScript, focusing on three different date formats.

Understanding String Date Formats

String dates come in different styles, and knowing how to handle each is essential. There are three main formats:

  • mm-dd-yyyy
  • yyyy-mm-dd
  • dd-mm-yyyy

Each format requires a tailored approach for conversion, and JavaScript offers an elegant solution—the new Date() constructor. This constructor can ingest a date string, timestamp, or number of milliseconds. For the purpose of this exploration, we focus on using strings as input.

How to convert mm-dd-yyyy date string to date object in javascript

The mm-dd-yyyy format is the default in JavaScript, making conversion a breeze. Just drop the string into the new Date() constructor, like this:

let date_string = "04/03/2020"; // Apr 03 2020
let output = new Date(date_string);
console.log(output.toString()); // Fri Apr 03 2020 00:00:00
Run Code

How to convert dd-mm-yyyy date string to date object in javascript

The dd-mm-yyyy format poses a challenge, as the new Date() constructor doesn't directly support it. Pre-processing becomes the key, as showcased in the following example:

let date_string = "03/04/2020"; // Apr 03 2020
let parts_of_date = date_string.split("/");
let output = new Date(+parts_of_date[2], parts_of_date[1] - 1, +parts_of_date[0]);
console.log(output.toString()); // Fri Apr 03 2020 00:00:00
Run Code

Notice in line number 3 we are using +parts_of_date[2] and +parts_of_date[0]. That is to convert a string to integer and pass in the constructor.

For the second parameter, we are subtracting 1. Because in javascript, the month starts from 0 and ranges between 0 to 11.

Convert yyyy-mm-dd date string to date object in javascript

The yyyy-mm-dd format is a JavaScript favorite. Just drop the string into the new Date() constructor, and you're good to go:

let date_string = "2020/04/03/"; // Apr 03 2020
let output = new Date(date_string);
console.log(output.toString()); // Fri Apr 03 2020 00:00:00
Run Code

Easy, right? Remember these small steps, specifically the dd-mm-yyyy string to date object. Many times we may get wrong date conversion issue after converting the date where it may be one day less than the actual input.

These type of issues can be solved by the same approach as described in the Convert dd-mm-yyyy format string to date in javascript section.

A Moment Solution for Consistent Date Handling

To achieve even more robust and flexible date handling in JavaScript, consider integrating the Moment.js library. Moment.js streamlines date manipulation by providing an extensive set of functions and methods, making it a powerful tool for ensuring accuracy and consistency in date conversions.

// Install Moment.js using npm
// npm install moment
// Import Moment.js into your project
const moment = require('moment');
// Example of using Moment.js to parse and format dates
let date_string = "03/04/2020"; // Apr 03 2020
let formatted_date = moment(date_string, "DD/MM/YYYY").format("ddd MMM DD YYYY HH:mm:ss");
console.log(formatted_date); // Fri Apr 03 2020 00:00:00

In the provided example, Moment.js effortlessly parses the input date string with the specified format ("DD/MM/YYYY") and then formats it according to the desired output format ("ddd MMM DD YYYY HH:mm:ss"). This streamlined approach not only simplifies the code but also ensures consistent and accurate results across various date formats. By incorporating Moment.js into your projects, you can significantly enhance the reliability and maintainability of your date-related operations.