-
In my application, a user provides a date in spoken language. so for example "16 August" "16th August" "16th of August" "August 16" etc etc should be converted to Date(2021/08/16) Is there a way to do that? P.S: I also need this to be in a locale other than English (specifically, Hebrew) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I don't believe that date-fns can handle natural language date parsing. You would have to pre-determine the expected patterns ahead of time and find a match by trying them all in the Fortunately, the scope of your problem makes it somewhat easy to solve with regular expressions. For fun, I've hacked at it a bit while drinking my morning coffee. I hope this sets you on the right path. If others find this useful, maybe this can be the foundation of a new date-fns function. https://codesandbox.io/s/natural-language-date-parsing-demo-6uzkk?file=/src/index.js If you want something more robust, Chrono looks pretty cool. There's no Hebrew locale, but maybe that's something you could contribute back. https://github.com/wanasit/chrono |
Beta Was this translation helpful? Give feedback.
I don't believe that date-fns can handle natural language date parsing. You would have to pre-determine the expected patterns ahead of time and find a match by trying them all in the
parse
function.Fortunately, the scope of your problem makes it somewhat easy to solve with regular expressions. For fun, I've hacked at it a bit while drinking my morning coffee. I hope this sets you on the right path. If others find this useful, maybe this can be the foundation of a new date-fns function. https://codesandbox.io/s/natural-language-date-parsing-demo-6uzkk?file=/src/index.js
If you want something more robust, Chrono looks pretty cool. There's no Hebrew locale, but maybe that's something you co…