What is chrono-node?
chrono-node is a natural language date parser in JavaScript. It can parse dates from various date formats and natural language text, making it useful for applications that need to understand and manipulate dates from user input.
What are chrono-node's main functionalities?
Parsing Dates from Natural Language
This feature allows you to parse dates from natural language text. In this example, the text 'Let's meet on Tuesday' is parsed to a Date object representing the next Tuesday.
const chrono = require('chrono-node');
const result = chrono.parseDate('Let's meet on Tuesday');
console.log(result);
Parsing Dates with Specific Time
This feature allows you to parse dates that include specific times. The text 'The event is at 5 PM on 20th October' is parsed to a Date object representing the specified date and time.
const chrono = require('chrono-node');
const result = chrono.parseDate('The event is at 5 PM on 20th October');
console.log(result);
Parsing Relative Dates
This feature allows you to parse relative dates. The text '2 days from now' is parsed to a Date object representing the date two days from the current date.
const chrono = require('chrono-node');
const result = chrono.parseDate('2 days from now');
console.log(result);
Customizing Parsers
This feature allows you to customize the parsers used by chrono-node. In this example, a custom parser is created and used to parse the text 'next Friday'.
const chrono = require('chrono-node');
const customParser = new chrono.Chrono();
customParser.parsers.push(new chrono.Parser());
const result = customParser.parseDate('next Friday');
console.log(result);
Other packages similar to chrono-node
date-fns
date-fns is a modern JavaScript date utility library that provides comprehensive, yet simple, functions for working with dates. Unlike chrono-node, which focuses on natural language parsing, date-fns offers a wide range of date manipulation and formatting functions.
moment
Moment.js is a widely-used library for parsing, validating, manipulating, and formatting dates. While it does not focus on natural language parsing like chrono-node, it provides extensive functionality for handling dates and times in JavaScript.
luxon
Luxon is a modern JavaScript library for working with dates and times, created by one of the Moment.js developers. It offers a more modern API and better support for internationalization compared to Moment.js, but like Moment.js, it does not specialize in natural language parsing.
Chrono
A natural language date parser in Javascript. It is designed to extract date informations from any given text.
Chrono supports a number of date and time formats, including :
- Today, Tomorrow, Yesterday, last Friday, etc
- 10/13/2013
- this Friday from 13:00 - 16.00
- Saturday, 17 August 2013 - Monday, 19 August 2013
- Sat Aug 17 2013 18:40:39 GMT+0900 (JST)
Node.js
npm install chrono-node
Browser
<script src="https://rawgithub.com/berryboy/chrono/master/chrono.min.js"></script>
USAGE
Just pass a string to function chrono.parseDate
or chrono.parse
.
> var chrono = require('chrono-node')
> chrono.parseDate('An appointment on Sep 12-13')
Thu Sep 12 2013 12:00:00 GMT+0900 (JST)
> chrono.parse('An appointment on Sep 12-13')
[ { start:
{ year: 2013,
month: 8,
day: 12,
impliedComponents: [Object],
isCertain: [Function],
date: [Function] },
startDate: Thu Sep 12 2013 12:00:00 GMT+0900 (JST),
end:
{ year: 2013,
month: 8,
day: 13,
impliedComponents: [Object],
isCertain: [Function],
date: [Function] },
endDate: Fri Sep 13 2013 12:00:00 GMT+0900 (JST),
referenceDate: Sat Aug 17 2013 17:54:57 GMT+0900 (JST),
index: 18,
text: 'Sep 12-13',
concordance: 'An appointment on Sep 12-13' } ]
Reference Date
Today's "Friday" is difference from last month's "Friday".
The meaning of mentioned dates are depended on when they were mentioned.
Chrono let you define the reference date using chrono.parse(text,ref)
and chrono.parseDate(text,ref)
.
> chrono.parseDate('Friday', new Date(2012,7,23));
Fri Aug 24 2012 12:00:00 GMT+0700 (ICT)
> chrono.parseDate('Friday', new Date(2012,7,1));
Fri Aug 03 2012 12:00:00 GMT+0700 (ICT)
Text
Chrono is designed to work with long text (notes, emails, articles, etc).
chrono.parse
will return an array of every date mentioned in the story.
> var text = 'October 7, 2011, of which details were not revealed out of respect to Jobs\'s family.[239] Apple announced on the same day that they had no plans for a public service, but were encouraging "well-wishers" to send their remembrance messages to an email address created to receive such messages.[240] Sunday, October 16, 2011'
> chrono.parse(text)
[{ start:
{ year: 2011,
month: 9,
day: 7,
....
{ start:
{ year: 2011,
month: 9,
day: 16,
....
Parsing Result
Chrono provides very detailed parsing results as objects of class chrono.ParseResult
.
start (chrono.DateComponents)
: The parsing result as a DateComponents objectstartDate (Date)
: The parsing result as a javascript Date objectend (chrono.DateComponents)
endDate (Date)
: Similar to start
and startDate
(Optional)index (int)
: The location within the input text of this resulttext (string)
: The mentioned words within the input text of this resultconcordance (string)
: The context of mentioned words within the input text (up to 30 characters)referenceDate (Date)
: The reference date of this result
Date Components
year
,month
,day
, dayOfWeek
, hour
, minute
, second
: The datected componentsimpliedComponents (array)
: The components that are not explicitly mentioneddate ( function )
: Return a javascript Date
Customize Chrono
Parser
Parser is a module for low-level parsing. Each parser is designed to handle a single specific date format.
In Chrono's parsing process, a number of parsers will be used togather to produce the results.
You should define new type of parsers for supporting new date formats or languages.
Chrono create parser objects by factory method pattern.
To add a new type of parser, declare a new factory function in chrono.parsers
.
Within that function:
- Create an object from
chrono.Parser()
- Override the object's
pattern
and extract
methods - Return the object
chrono.parsers.ChrismasParser = function(text, ref, opt) {
var parser = chrono.Parser(text, ref, opt);
parser.pattern = function () { return /Christmas/i }
parser.extract = function(text, index) {
var mentioned_text = text.substr(index).match(/Christmas/i)[0];
return new chrono.ParseResult({
referenceDate : ref,
text : mentioned_text,
index: index,
start: {
day: 25, month: 11,
year: ref.getFullYear()
impliedComponents: ['year']
}
});
}
return parser;
}
...
> chrono.parseDate("I'll arrive at 2.30AM on Christmas night")
Wed Dec 25 2013 02:30:00 GMT+0900 (JST)