Here's a possible solution using regular expressions and the Intl object from JavaScript:
index.tsx987 chars29 lines
This function takes a string as input and tries to match it against an array of common date formats from around the world. If a match is found, it uses the Intl object to correctly format the date according to the user's locale, and returns a JavaScript Date object. If no match is found, the function returns null.
Here's an example usage of the function:
index.tsx157 chars5 lines
In this example, the input string is in the "DD-MM-YYYY" format, which matches one of the formats in the DATE_FORMATS array. Therefore, the function correctly parses the date and returns a JavaScript Date object.
gistlibby LogSnag