How to parse a “dd/mm/yyyy” or “dd-mm-yyyy” or “dd-mmm-yyyy” formatted date string using JavaScript?

Sometimes, we want to parse a “dd/mm/yyyy” or “dd-mm-yyyy” or “dd-mmm-yyyy” formatted date string using JavaScript.

In this article, we’ll look at how to parse a “dd/mm/yyyy” or “dd-mm-yyyy” or “dd-mmm-yyyy” formatted date string using JavaScript.

How to parse a “dd/mm/yyyy” or “dd-mm-yyyy” or “dd-mmm-yyyy” formatted date string using JavaScript?

To parse a “dd/mm/yyyy” or “dd-mm-yyyy” or “dd-mmm-yyyy” formatted date string using JavaScript, we use moment.js.

For instance, we write

const day = moment("12-25-1995", "MM-DD-YYYY");

to call moment with the date string we want to parse and a string with the format of the date string being parsed.

MM stands for the 2 digit month.

DD stands for the 2 digit day.

And YYYY stands for the 4 digit year.

Conclusion

To parse a “dd/mm/yyyy” or “dd-mm-yyyy” or “dd-mmm-yyyy” formatted date string using JavaScript, we use moment.js.