Convert relative time to datetime in Javascript

1.9k Views Asked by At

How can I convert a relative date/time string to a real date/time in Javascript (can use libraries)

For example, the user will input into an edit field any of the following examples (among others): THIS IS THE INPUT:

  • 2 weeks ago
  • Last 3 weeks
  • 24 hours ago
  • Last 4 months
  • Last week
  • Last monday

So, taking now/todays datetime as a starting point, I need to get the date/time they are referring to. So is today is 2018-04-06 11:19 and they enter '1 week ago' or 'a week ago' then I need a routine which will return

  • '2018-03-30 11:19'

as the OUTPUT

I know about moment.js and how to use that to change a date/time/moment to a relative datetime, but I need it done the other way around, - to change a relative date string to a date/time.

EDIT: I have now found : - https://github.com/wanasit/chrono

which seems to solve the problem.

1

There are 1 best solutions below

0
Tr-evor H-awes On

https://github.com/wanasit/chrono This library seems to provide exactly as was requested in the OP. The input is a relative date in Natural Language (almost) and the output is a date.