0

I am creating a Speech to Text input engine where user will say input values for html forms. One of the input field is a type="date". Suppose the user says "July 20th 2021", Is it possible to change this date into JavaScript date format? Is there any library that does this kind of conversion?

Thanks in advance.

abas
  • 19
  • 5
  • `moment.js` can parse many date formats, it can probably do this. – Barmar Aug 03 '21 at 20:00
  • For this specific example: `new Date("July 20th 2021")` is invalid, but `new Date("July 20 2021")`, so using regex to replace 1st, 2nd, 3rd, 4th etc. with 1, 2, 3, 4 etc. would work. – kol Aug 03 '21 at 20:02
  • Formats that moment.js can parse: https://momentjs.com/docs/#/parsing/string/ – kol Aug 03 '21 at 20:03
  • you are right, moment.js does parse that string. I had to remove the text after the date like 'th', 'nd', 'rd'. I dont think this will happen alot, but the moment.js does not parse if the date includes those substring. Thanks. – abas Aug 03 '21 at 20:17

1 Answers1

0

I would start with the Date.parse() function. If that doesn't work for you then your best bets are likely date-fns or momentJS but if your format isn't one that is immediately parseable with one of the built in functions then you may need to a bit of pre-processing yourself.

David Kidwell
  • 600
  • 1
  • 6
  • 15
  • No, do not start with Date.parse unless you are using one of the two formats supported by ECMA-262. Otherwise, it's probably the worst approach. Manual parsing is perhaps 2 or 3 lines of code, a library can help but really isn't necessary to parse a single format. There are many, many good parsing and formatting libraries (e.g. [*fecha.js*](https://github.com/taylorhakes/fecha)) that are very much smaller than either date-fns or moment.js (which is now considered a legacy project, [*Luxon*](https://moment.github.io/luxon/#/) is the replacement). – RobG Aug 03 '21 at 21:40