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?
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?
- similar question here: stackoverflow./questions/1003326/… – arturomp Commented May 9, 2013 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 Commented May 9, 2013 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 Commented May 9, 2013 at 17:54
2 Answers
Reset to default 9date.js can parse human readable dates:
http://www.datejs./
SugarJS can also parse natural language dates:
http://sugarjs./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?
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:
- Include this script: https://www.unpkg./chrono-node/dist/bundle.js
- Use the global variable
chrono
in your script (available methods here)
E.g. chrono.parseDate('Tomorrow at 4 PM PST')