Chrono Node
Chrono Node (chrono-node) is a natural language date and time parser for JavaScript, currently at version 2.9.0. It allows developers to extract and interpret date and time information from arbitrary text, supporting a wide range of formats including relative phrases ("tomorrow," "5 days ago"), absolute dates ("17 August 2013"), and date ranges ("Sep 12-13"). Since its v2 rewrite, the library is implemented in TypeScript, offering a more modular architecture with distinct parser and refiner interfaces. It differentiates itself by focusing on a performant, native JavaScript date/time core (having removed `dayjs` in v2.9.0) and providing robust control over parsing context, including explicit reference dates and timezones. While previous versions attempted to parse all known languages by default, v2.0.0 and later default to international English, requiring explicit configuration for other supported locales (e.g., Japanese, French, Dutch, Russian, Ukrainian).
Common errors
-
TypeError: chrono.ParsingComponent.dayjs is not a function
cause Upgrading to chrono-node v2.9.0 without updating code that used the `dayjs` integration. The `dayjs` dependency and its related functions were removed in v2.9.0.fixRemove any calls to `ParsingComponent.dayjs()` or other `dayjs`-specific logic from your custom parsers or integrations. Refactor to use native JavaScript `Date` methods. -
Error: Cannot find module 'chrono-node' or undefined is not a function/object for chrono
cause Incorrect import statement for ESM or CommonJS modules, or trying to use named imports when the library uses a namespace export, especially in mixed environments.fixFor ESM, use `import * as chrono from 'chrono-node';`. For CommonJS, use `const chrono = require('chrono-node');`. Ensure your build configuration (webpack, rollup, TypeScript `moduleResolution`) correctly handles module types. -
Date results are in the past when expecting future dates (e.g., 'Friday' parses to last Friday)
cause The parser defaults to the closest matching date. If 'Friday' has already passed in the current week relative to the reference date, it will pick the past Friday.fixPass the `forwardDate: true` option in the `ParsingOption` object: `chrono.parseDate('Friday', referenceDate, { forwardDate: true });` -
Parsing results for relative dates (e.g., 'today', 'tomorrow') are incorrect when timezones are involved.
cause The reference date's timezone might not be correctly applied during interpretation, especially when the system timezone differs from the intended reference timezone. This was explicitly fixed in v2.8.0 for some scenarios.fixAlways provide a comprehensive `ParsingReference` object with both `instant` (a `Date` object) and `timezone` (a string like 'America/Los_Angeles' or a minute offset) to ensure consistent timezone-aware parsing.
Warnings
- breaking Chrono v2.0.0 changed its default behavior from attempting to parse all known languages to only international English. Code relying on auto-detection or parsing non-English text without explicit locale configuration will produce incorrect results or fail to parse.
- breaking Version 2.9.0 removed the `dayjs` dependency and consequently deprecated and removed `ParsingComponent.dayjs()`. Any custom parsers or existing code relying on this function will break.
- gotcha Incorrect date interpretation for relative terms (e.g., 'tomorrow', 'next Friday') can occur if the `ParsingReference` is not correctly set. The meaning of such terms is highly dependent on the `instant` and `timezone` provided in the reference.
- gotcha The `forwardDate` parsing option, when `true`, forces Chrono to interpret relative dates (like 'Friday' or 'next month') as being in the future relative to the `reference` date. If not set, it may resolve to a past date if it's closer.
- gotcha Version 2.8.0 introduced a fix where reference date calculations (e.g., for "1 day ago", "tomorrow at 9am") are now based on the *assigned timezone* of the reference instant/timestamp, rather than the system's local timezone. This change might subtly alter results for users relying on implicit system timezone behavior.
Install
-
npm install chrono-node -
yarn add chrono-node -
pnpm add chrono-node
Imports
- chrono
import chrono from 'chrono-node';
import * as chrono from 'chrono-node';
- parseDate
import { parseDate } from 'chrono-node/dist/chrono.js';import { parseDate } from 'chrono-node'; - ParsedResult, ParsingReference, ParsingOption
import { ParsedResult, ParsingReference, ParsingOption } from 'chrono-node';import type { ParsedResult, ParsingReference, ParsingOption } from 'chrono-node'; - chrono (CommonJS)
const chrono = require('chrono-node');
Quickstart
import * as chrono from 'chrono-node';
import type { ParsingReference, ParsingOption } from 'chrono-node';
const text1 = 'An appointment on Sep 12-13 from 2 PM to 4 PM next Friday';
const text2 = 'Tomorrow at noon in New York';
const text3 = 'In 3 days';
// Basic parsing
const result1 = chrono.parseDate(text1);
console.log('Result 1 (basic):', result1?.toISOString());
// Parsing with a specific reference date and timezone
const referenceInstant = new Date('2025-02-27T10:00:00Z'); // UTC instant
const referenceOptions: ParsingReference = {
instant: referenceInstant,
timezone: 'America/Los_Angeles' // Reference timezone for interpretation
};
const result2 = chrono.parseDate(text2, referenceOptions);
console.log('Result 2 (ref + timezone):', result2?.toISOString());
// Parsing with the 'forwardDate' option to ensure future dates
const today = new Date('2025-03-01T12:00:00Z'); // Saturday, March 1, 2025
const forwardOptions: ParsingOption = { forwardDate: true };
const result3 = chrono.parseDate(text3, today, forwardOptions);
console.log('Result 3 (forwardDate):', result3?.toISOString());
// Full parsed result (including text, index, start/end components)
const detailedResult = chrono.parse('Book a meeting for next Tuesday at 3pm', new Date('2025-03-01T00:00:00Z'), { forwardDate: true });
if (detailedResult.length > 0) {
console.log('Detailed result:', JSON.stringify(detailedResult[0], null, 2));
console.log(' Extracted text:', detailedResult[0].text);
console.log(' Parsed date:', detailedResult[0].date().toISOString());
}