chrono-node
A natural language date parser in Javascript
Last updated 10 hours ago by wanasit .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install chrono-node 
SYNC missed versions from official npm registry.

Chrono (v2)

A natural language date parser in Javascript.

Build Status Coverage Status

It is designed to handle most date/time format and extract information from any given text:

  • Today, Tomorrow, Yesterday, Last Friday, etc
  • 17 August 2013 - 19 August 2013
  • This Friday from 13:00 - 16.00
  • 5 days ago
  • 2 weeks from now
  • Sat Aug 17 2013 18:40:39 GMT+0900 (JST)
  • 2014-11-30T08:15:30-05:30

Installation

With npm:

$ npm install --save chrono-node
import * as chrono from 'chrono-node';

chrono.parseDate('An appointment on Sep 12-13'); 

For Node.js:

const chrono = require('chrono-node');

// or `import chrono from 'chrono-node'` for ECMAScript

What's changed in the v2

For Users

  • Chrono’s default now handles only international English. While in the previous version, it tried to parse with all known languages.
  • The current supported languages are only en and ja. Other languages from v1 (de, fr, nl, pt, and zh) are under development.

For contributors and advanced users

  • The project is rewritten in TypeScript
  • New Parser and Refiner interface
  • New source code structure. All parsers, refiners, and configuration should be under a locale directory (See. locales/en)

Note: v1.x.x will still be supported for the time being.

Usage

Simply pass a string to functions chrono.parseDate or chrono.parse.

import * as chrono from 'chrono-node';

chrono.parseDate('An appointment on Sep 12-13');
// Fri Sep 12 2014 12:00:00 GMT-0500 (CDT)
    
chrono.parse('An appointment on Sep 12-13');
/* [{ 
    index: 18,
    text: 'Sep 12-13',
    start: ...
}] */

For more advanced usage, here is the typescript definition of the parse function:

parse(text: string, refDate?: Date, option?: ParsingOption): ParsedResult[] {...}

Reference Date

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 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)

Parsing Options

forwardDate (boolean) to assume the results should happen after the reference date (forward into the future)

const referenceDate = new Date(2012, 7, 25);
// Sat Aug 25 2012 00:00:00 GMT+0900 -- The reference date was Saturday

chrono.parseDate('Friday', referenceDate);
// Fri Aug 24 2012 12:00:00 GMT+0900 (JST) -- The day before was Friday

chrono.parseDate('Friday', referenceDate, { forwardDate: true });
// Fri Aug 31 2012 12:00:00 GMT+0900 (JST) -- The following Friday

Parsed Results and Components

ParsedResult

  • refDate: Date The reference date of this result
  • index: number The location within the input text of this result
  • text: string The text this result that appears in the input
  • start: ParsedComponents The parsed date components as a ParsedComponents object
  • end?: ParsedComponents Similar to start
  • date: () => Date Create a javascript Date

ParsedComponents

  • get(component) Get known or implied value for the component
  • date() Create a javascript Date

For example:

const results = chrono.parse('I have an appointment tomorrow from 10 to 11 AM');

results[0].index;     // 15
results[0].text;      // 'tomorrow from 10 to 11 AM'
results[0].refDate;   // Sat Dec 13 2014 21:50:14 GMT-0600 (CST)

// `start` is Sat Dec 14 2014 10:00:00
results[0].start.get('day');    // 14 (the 14th, the day after refDate)
results[0].start.get('month');  // 12 (or December)
results[0].start.get('hour');   // 10 
results[0].start.date();        // Sun Dec 14 2014 10:00:00 GMT-0600 (CST)

...
results[0].end.date();  // Sun Dec 14 2014 11:00:00 GMT-0600 (CST)

Strict vs Casual configuration

Chrono comes with strict mode that parse only formal date patterns.

// 'strict' mode
chrono.strict.parseDate('Today');       // null
chrono.strict.parseDate('Friday');      // null
chrono.strict.parseDate('2016-07-01');  // Fri Jul 01 2016 12:00:00 ...
chrono.strict.parseDate('Jul 01 2016'); // Fri Jul 01 2016 12:00:00 ...

// 'casual' mode (default) 
chrono.parseDate('Today');              // Thu Jun 30 2016 12:00:00 ...
chrono.casual.parseDate('Friday');      // Fri Jul 01 2016 12:00:00 ...
chrono.casual.parseDate('2016-07-01');  // Fri Jul 01 2016 12:00:00 ...
chrono.casual.parseDate('Jul 01 2016'); // Fri Jul 01 2016 12:00:00 ...

Locales

By default, Chrono is configured to handle only international English. This differs from the previous version of Chrono that would try all locales by default.

There are several locales supported contributed by multiple developers under ./locales directory.

// default English (US)
chrono.parseDate('6/10/2018');    

chrono.en.parseDate('6/10/2018'); 
chrono.ja.parseDate('昭和64年1月7日'); 

Current supported locale options are: en, ja

Customize Chrono

Chrono’s extraction pipeline configuration consists of parsers: Parser[] and refiners: Refiner[].

  • First, each parser independently extracts patterns from input text input and create parsing results (ParsingResult).
  • Then, the parsing results are combined, sorted, and refined with the refiners. In the refining phase, the results can be filtered-out, merged, or attached with additional information.

Parser

interface Parser {
    pattern: (context: ParsingContext) => RegExp,
    extract: (context: ParsingContext, match: RegExpMatchArray) =>
        (ParsingComponents | ParsingResult | {[c in Component]?: number} | null)
}

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 create a new parser for supporting new date formats or languages by providing RegExp pattern pattern() and extracting result or components from the RegExp match extract().

const custom = chrono.casual.copy();
custom.parsers.push({
    pattern: () => { return /\bChristmas\b/i },
    extract: (context, match) => {
        return {
            day: 25, month: 12
        }
    }
});

custom.parseDate("I'll arrive at 2.30AM on Christmas night");
// Wed Dec 25 2013 02:30:00 GMT+0900 (JST)
// 'at 2.30AM on Christmas'

Refiner

interface Refiner {
    refine: (context: ParsingContext, results: ParsingResult[]) => ParsingResult[]
}

Refiner is a higher level module for improving or manipulating the results. User can add a new type of refiner to customize Chrono's results or to add some custom logic to Chrono.

const custom = chrono.casual.copy();
custom.refiners.push({
    refine: (context, results) => {
        // If there is no AM/PM (meridiem) specified,
        //  let all time between 1:00 - 4:00 be PM (13.00 - 16.00)
        results.forEach((result) => {
            if (!result.start.isCertain('meridiem') &&
                result.start.get('hour') >= 1 && result.start.get('hour') < 4) {

                result.start.assign('meridiem', 1);
                result.start.assign('hour', result.start.get('hour') + 12);
            }
        });
        return results;
    }
});

// This will be parsed as PM.
// > Tue Dec 16 2014 14:30:00 GMT-0600 (CST) 
custom.parseDate("This is at 2.30");

// Unless the 'AM' part is specified
// > Tue Dec 16 2014 02:30:00 GMT-0600 (CST)
custom.parseDate("This is at 2.30 AM");

In the example, the custom refiner assigns 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).

Development Guides

This guide explains how to set up chrono project for prospective contributors.

# Clone and install library
$ git clone https://github.com/wanasit/chrono.git chrono
$ cd chrono
$ npm install

Parsing date from text is complicated. 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.

Chrono's unit testing is based-on Jest.

# Run the test
$ npm run test

# Run the test in watch mode
$ npm run watch

Chrono's source files is in src directory. The built bundle (dist/*) is created by running Webpack via the following command

$ npm run build

Current Tags

  • 2.1.3                                ...           latest (10 hours ago)

58 Versions

  • 2.1.3                                ...           10 hours ago
  • 1.4.7                                ...           11 hours ago
  • 2.1.2                                ...           12 hours ago
  • 2.1.1 [deprecated]           ...           13 hours ago
  • 2.1.0                                ...           a day ago
  • 2.0.3                                ...           a day ago
  • 2.0.2                                ...           4 days ago
  • 2.0.1 [deprecated]           ...           4 days ago
  • 2.0.0 [deprecated]           ...           5 days ago
  • 1.4.6                                ...           2 months ago
  • 1.4.5                                ...           3 months ago
  • 1.4.4                                ...           4 months ago
  • 1.4.3                                ...           5 months ago
  • 1.4.2                                ...           6 months ago
  • 1.3.11                                ...           a year ago
  • 1.3.5                                ...           3 years ago
  • 1.3.4                                ...           3 years ago
  • 1.3.3                                ...           3 years ago
  • 1.3.2                                ...           3 years ago
  • 1.3.1                                ...           3 years ago
  • 1.2.5                                ...           4 years ago
  • 1.2.4                                ...           4 years ago
  • 1.2.3                                ...           4 years ago
  • 1.2.2                                ...           4 years ago
  • 1.2.1                                ...           4 years ago
  • 1.2.0                                ...           4 years ago
  • 1.1.6                                ...           4 years ago
  • 1.1.5                                ...           4 years ago
  • 1.1.4                                ...           4 years ago
  • 1.1.3                                ...           4 years ago
  • 1.1.2                                ...           5 years ago
  • 1.1.1                                ...           5 years ago
  • 1.0.11                                ...           5 years ago
  • 1.0.10                                ...           5 years ago
  • 1.0.9                                ...           5 years ago
  • 1.0.8                                ...           5 years ago
  • 1.0.7                                ...           5 years ago
  • 1.0.6                                ...           5 years ago
  • 1.0.5                                ...           5 years ago
  • 1.0.4                                ...           5 years ago
  • 1.0.3                                ...           5 years ago
  • 1.0.2                                ...           6 years ago
  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
  • 0.1.11                                ...           6 years ago
  • 0.1.10                                ...           6 years ago
  • 0.1.8                                ...           7 years ago
  • 0.1.7                                ...           7 years ago
  • 0.1.6                                ...           7 years ago
  • 0.1.5                                ...           7 years ago
  • 0.1.4                                ...           7 years ago
  • 0.1.3                                ...           7 years ago
  • 0.1.2                                ...           7 years ago
  • 0.1.1                                ...           7 years ago
  • 0.0.4                                ...           7 years ago
  • 0.0.3                                ...           7 years ago
  • 0.0.2                                ...           8 years ago
  • 0.0.1                                ...           8 years ago
Maintainers (1)
Downloads
Today 195
This Week 637
This Month 767
Last Day 126
Last Week 212
Last Month 743
Dependencies (1)
Dev Dependencies (4)
Dependents (215)

Copyright 2014 - 2016 © taobao.org |