DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 964,423 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
anjan-dutta
anjan-dutta

Posted on • Updated on

Ways to convert string to date in Javascript

This article was originally posted in my blog.

A string date can be of three types and, we will convert each of those to a date object in javascript.
Formats of stringΒ date:

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

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

We can pass the yyyy-mm-dd format string directly in the new Date() constructor as below example.

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 
Enter fullscreen mode Exit fullscreen mode

Next two conversions are,

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

Read more....

Top comments (0)

Need a better mental model for async/await?

Check out this classic DEV post on the subject.

β­οΈπŸŽ€ JavaScript Visualized: Promises & Async/Await

async await