1

Moment.js does humanized and calendar dates, for example:

moment().calendar()
"Today at 10:17 AM"

which gets a current date object and converts to a calendar date, and

is there any way to do that in reverse? Like if I give it "Today at 10:17 AM", it returns a date object with todays date and 10:17 AM as the time?

Agustin Meriles
  • 4,866
  • 3
  • 29
  • 44
user864572
  • 191
  • 3
  • 13
  • similar question here: http://stackoverflow.com/questions/1003326/is-there-a-natural-language-parser-for-date-times-in-javascript – arturomp May 09 '13 at 15:30
  • Yeah, sorry, didn't search natural language. Though the answer there (and the only reply here) doesn't handle day + time – user864572 May 09 '13 at 17:11
  • absolutely! didn't mean that in a bad way, I just thought it'd be nice to know about it given the number of contributions ;) – arturomp May 09 '13 at 17:54

2 Answers2

9

date.js can parse human readable dates:

http://www.datejs.com/

SugarJS can also parse natural language dates:

http://sugarjs.com/dates

It can deal with stuff like:

one day before yesterday
2 days after monday
2 weeks from monday
a second ago
25 years from now
last wednesday

Also, see this related question: Is there a natural language parser for date/times in javascript?

Community
  • 1
  • 1
Robert Kajic
  • 8,689
  • 4
  • 44
  • 43
  • Sorry I had seaerched human readable and only got suggestions of datejs, which hasnt been updated in years and is still apparently in alpha. Forgot to search natural language. Sugarjs looks great! Thanks! Though it doesnt seem to work on day + time like "next thursday at 11" but I can add that parsing on top. – user864572 May 09 '13 at 16:59
  • One of the things I found out along the way (and it took me a ridiculous amount of time to find out what was going on) is that the `addHours` operations and alike are actually changing the underlying object instead of creating a new one. :-( – Wilfred Springer Jan 28 '14 at 08:46
  • SugarJS doesn't parse timezones :/ the "PST" in "tomorrow at 4 PM PST" [is ignored](https://sugarjs.com/dates/#/Timezones). – Prid Jun 28 '22 at 17:59
1

Chrono v2

It's the only library I've found that also parses timezones.

Examples


Unfortunately, the only documented way of using is via NPM or as ES6 Module, but I found a way to use the library with the traditional script tag approach.

Non-NPM / non-module usage:

  1. Include this script: https://www.unpkg.com/chrono-node/dist/bundle.js
  2. Use the global variable chrono in your script (available methods here)

E.g. chrono.parseDate('Tomorrow at 4 PM PST')

Prid
  • 1,272
  • 16
  • 20