1

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • See moment [`calendar()`](https://momentjs.com/docs/#/displaying/calendar-time/) and please show what did you tried so far. – VincenzoC Apr 06 '18 at 09:25
  • Thanks. calendar() seems to be for display, rather than parsing. I have been looking for a library to parse relative dates to a normal date string but cant find any yet. – Tr-evor H-awes Apr 06 '18 at 09:32
  • I have now found : - https://github.com/wanasit/chrono which seems to solve the problem. – Tr-evor H-awes Apr 06 '18 at 09:56
  • Check this: https://stackoverflow.com/questions/563406/add-days-to-javascript-date – FDavidov Apr 06 '18 at 10:05

1 Answers1

0

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.