Skip to content
Advertisement

Invalid date when parsing with locale it

I need to parse a date in the “it” locale with momentjs, and I’m doing this

JavaScript

What I get is “invalid date” and I don’t understand why. Can you help me?

Using the “en” locale (with the date written as 12/20/2018) all is ok

Advertisement

Answer

The below snippet will accomplish what you want. It takes moment’s date format for a given local and passes it to the constructor when creating a moment.

With that said, the comments above raise a lot of good points and this is not a reliable way to be handling dates.

For example, if someone in Italy entered a date string in the en MM/DD/YYYY format this would break

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement