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.
INSTALLATION
Node.js
npm install chrono-node
Browser
<script src="https://raw.github.com/berryboy/chrono/master/chrono.min.js"></script>
USAGE
Basic
You can parse strings containing a natural language date using the chrono.parseDate
or chrono.parse
method.
> var chrono = require('chrono-node');
> chrono.parseDate('Today');
Thu Aug 23 2012 12:00:00 GMT+0700 (ICT)
> chrono.parse('Today');
[ { start:
{ year: 2012,
month: 7,
day: 23,
hour: undefined,
minute: undefined,
second: undefined,
date: [Function] },
startDate: Thu Aug 23 2012 12:00:00 GMT+0700 (ICT),
referenceDate: Thu Aug 23 2012 01:17:46 GMT+0700 (ICT),
index: 0,
text: 'Today',
sentence: undefined } ]
Reference Date
Today's "Friday" is difference from last month's "Friday". The meaning of mentioned dates are depended on when were they 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)
Reference
Chrono is also designed to handle a long text. chrono.parse
will return a array of every date mentioned in the input text. chrono.parseDate
will return the first one.
> 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,
hour: undefined,
minute: undefined,
second: undefined,
date: [Function] },
startDate: Fri Oct 07 2011 12:00:00 GMT+0700 (ICT),
referenceDate: Thu Aug 23 2012 01:48:37 GMT+0700 (ICT),
index: 0,
text: 'October 7, 2011',
sentence: undefined },
{ start:
{ year: 2011,
month: 9,
day: 16,
hour: undefined,
minute: undefined,
second: undefined,
date: [Function] },
startDate: Sun Oct 16 2011 12:00:00 GMT+0700 (ICT),
referenceDate: Thu Aug 23 2012 01:48:37 GMT+0700 (ICT),
index: 297,
text: 'Sunday, October 16, 2011',
sentence: undefined } ]
EXAMPLES
Chrono can parse a number of date and time formats. Following are examples of strings that can be parsed properly.
- yesterday
- today
- tomorrow
- thursday
- friday 13:00
- friday 1pm - saturday 5am
- next tuesday
- last night
- last friday at 20:00
- tomorrow at 6:45pm
- 12/1/2012
- 12 - 13 Jan 2012
- 12 Jan - 13 Feb 2012
- Jan 12 - Feb 13
- 2012-3-12