Quantastical Quantastical -4 years ago 85
Javascript Question

Parse Date "01/01/2016 Fri"

I am trying to parse a date in JavaScript, but the particular format is giving me fits. I have exported data from my credit card company and the format of the date field is not compatible with

Date.parse
or
moment().isValid()
.

E.g.

Date.parse("01/01/2016 Fri") // NaN
moment("01/01/2016 Fri") // false


I'm not sure if I should do something with a RegEx
.test()
or
.matches()
because this is being used for a CSV import utility where dates may be in different formats. I was surprised the utility functions above didn't work.

Answer Source

Look in the Moment docs to see how to parse a date in any format. The first argument is the date string, the second is the format string. Alphanumeric characters are ignored, so you don't need to worry about slashes vs. dashes.

moment("01/01/2016 Fri", "MM-DD-YYYY ddd)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download