Converting Human-Entered Dates Into a Date Object

What would be the most foolproof method of converting dates (that is for a project I am working on that uses a ML text extractor) that humans enter into a Date() object.

This seems like quite a simple issue, but I can’t find a foolproof solution. This is because the formats vary so widely between people entering dates like "Feburary 20, 2021" to "1/1/20", as well as ones that use different formats like "2021/1/3".

Is there a prebuilt package that someone knows about?

EDIT: After some digging and experimenting, the only solution I found was to use moment.js (thanks to cWerning for the suggestion, as well as others who suggested other ideas) and input multiple templates like this:

moment(**date**,["YYYY/MM/DD","DD/MM/YYYY",etc... with other formats]

17 thoughts on “Converting Human-Entered Dates Into a Date Object”

Leave a Comment