Chrono supports most date and time formats, such as :
npm install chrono-node
bower install chrono
Chrono's modules are linked and packaged using Browserify on
src/chrono.js. By default,
chrono.js file exports
chrono object as a window global.
browserify src/chrono.js --s chrono -o chrono.js
Simply pass a string to function
> var chrono = require'chrono-node'> chronoparseDate'An appointment on Sep 12-13'Fri Sep 12 2014 12:00:00 GMT-0500 CDT> chronoparse'An appointment on Sep 12-13';index: 18text: 'Sep 12-13'tags: ENMonthNameMiddleEndianParser: truestart:knownValues: ObjectimpliedValues: Objectend:knownValues: ObjectimpliedValues: Object
Today's "Friday" is different from last month's "Friday".
The meaning of the referenced dates depends on when they are mentioned.
Chrono lets you define a reference date using
chrono.parse(text, ref) and
> chronoparseDate'Friday' 2012723;Fri Aug 24 2012 12:00:00 GMT+0700 ICT> chronoparseDate'Friday' 201271;Fri Aug 03 2012 12:00:00 GMT+0700 ICT
chrono.parse returns detailed parsing results as objects of class
var results = chronoparse'I have an appointment tomorrow from 10 to 11 AM'results0index // 15results0text // 'tomorrow from 10 to 11 AM'results0ref // Sat Dec 13 2014 21:50:14 GMT-0600 (CST)results0startdate // Sun Dec 14 2014 10:00:00 GMT-0600 (CST)results0enddate // Sun Dec 14 2014 11:00:00 GMT-0600 (CST)
startThe parsed date components as a ParsedComponents object
startbut can be null.
indexThe location within the input text of this result
textThe text this result that appears in the input
refThe reference date of this result
A group of found date and time components (year, month, hour, etc). ParsedComponents objects consist of
assign(component, value)Set known value to the component
imply(component, value)Set implied value to the component
get(component)Get known or implied value for the component
isCertain(component)return true if the value of the component is known.
Chrono’s extraction pipeline are mainly separated into 'parse' and ‘refine’ phases. During parsing, ‘parsers’ (
Parser) are used to extract patterns from the input text. The parsed results (ParsedResult) are the combined, sorted, then refine using ‘refiners’ (
Refiner). In the refining phase, the results can be combined, filtered-out, or attached with additional information.
Parser is a module for low-level pattern-based parsing. Ideally, each parser should be designed to handle a single specific date format. User can add new type of parsers for supporting new date formats or languages.
var christmasParser = ;// Provide search patternreturn /Christmas/i// This function will be called when matched pattern is found// Return a parsed result, that is 25 Decemberreturnref: reftext: match0index: matchindexstart:day: 25month: 12;var custom = ;customparserspushchristmasParser;customparseDate"I'll arrive at 2.30AM on Christmas night"// Wed Dec 25 2013 02:30:00 GMT+0900 (JST)
To create a custom parser, override
extract methods on an object of class
patternmethod must return
RegExpobject of searching pattern.
extractmethod will be called with the match object when the pattern is found. This function must create and return a result (or null to skip).
Refiner is a heigher level module for improving or manipurating the results. User can add a new type of refiner to customize Chrono's results or to add some custom logic to Chrono.
var guessPMRefiner = ;// If there is no AM/PM (meridiem) specified,// let all time between 1:00 - 4:00 be PM (13.00 - 16.00)resultsforEachif !resultstartisCertain'meridiem'&& resultstartget'hour' >= 1 && resultstartget'hour' < 4resultstartassign'meridiem' 1;resultstartassign'hour' resultstartget'hour' + 12;;return results;var custom = ;customrefinerspushguessPMRefiner;// This will be parsed as PM.// > Tue Dec 16 2014 14:30:00 GMT-0600 (CST)customparseDate"This is at 2.30";// Unless the 'AM' part is specified// > Tue Dec 16 2014 02:30:00 GMT-0600 (CST)customparseDate"This is at 2.30 AM";
In the example, a custom refiner is created for assigning PM to parsing results with ambiguous meridiem. The
refine method of the refiner class will be called with parsing results (from parsers or other previous refiners). The method must return an array of the new results (which, in this case, we modified those results in place).
This guild explains how to setup chrono project for prospective contributors.
# Clone and install librarygit clone chronocd chrononpm install# Try running the testnpm run test
Chrono's source files is in
src directory. The built bundle (
chrono.min.js) can be built by Browserify on
src/chrono.js using the following command
npm run make
Parsing date from text is complicated. Sometimes, a small change can have effects on unexpected places. So, Chrono is a heavily tested library. Commits that break a test shouldn't be allowed in any condition.
# Start karmanpm run karma# Start watch (run on a different terminal)npm run watch