Elasticsearch Date Math Parser
datemath-parser is a JavaScript library designed to parse date math expressions compatible with the Elasticsearch format. It takes expressions like `now+1h`, `now+1M/d`, or `2012-01-01||+1M/d` and returns an integer representing the timestamp in milliseconds. The current stable version is 1.0.6. This package appears to be largely unmaintained, with its last update occurring in 2018. While functional for its specific purpose, developers should be aware of its inactive development cycle when considering it for new projects or environments with strict maintenance requirements. It differentiates itself by adhering strictly to Elasticsearch's date math specification, making it a targeted solution for systems interacting with Elasticsearch date queries. Other, more actively maintained libraries, like `@elastic/datemath` or `date-fns` may offer broader date manipulation capabilities and better support.
Common errors
-
TypeError: require is not a function
cause Attempting to import `datemath-parser` using an `import` statement in an ECMAScript Module (ESM) environment.fixChange your import statement to use CommonJS `require()`: `const parser = require('datemath-parser');` or `const { parse } = require('datemath-parser');` -
ReferenceError: parser is not defined
cause The `datemath-parser` module or its `parse` function was not correctly imported or required before being used.fixEnsure you have `const parser = require('datemath-parser');` at the top of your file where `parser` is used, or `const { parse } = require('datemath-parser');` if you want to destructure the `parse` function directly. -
Error: Invalid Date Math String
cause The input string provided to `parser.parse()` does not conform to the expected Elasticsearch date math format.fixReview the Elasticsearch date math documentation (e.g., `now+1h`, `2023-01-01||+1M/d`) and correct your date math expression. Ensure correct units (`y`, `M`, `w`, `d`, `h`, `m`, `s`) and operators (`+`, `-`, `/`).
Warnings
- gotcha The `datemath-parser` package has not been updated since 2018, indicating it is no longer actively maintained. This means it may not receive security patches, bug fixes, or updates for compatibility with newer Node.js versions or JavaScript features.
- breaking This package is exclusively a CommonJS module. Using `import` statements in a pure ECMAScript Module (ESM) environment (e.g., in a Node.js project with `"type": "module"` or browser ESM) will result in a `TypeError: require is not a function` or similar module resolution errors.
- gotcha The library does not ship with TypeScript type definitions. Developers using TypeScript will lack type safety, IntelliSense, and compilation checks when interacting with `datemath-parser`, unless custom declarations are provided.
Install
-
npm install datemath-parser -
yarn add datemath-parser -
pnpm add datemath-parser
Imports
- parser
import parser from 'datemath-parser';
const parser = require('datemath-parser'); - parse
import { parse } from 'datemath-parser';const { parse } = require('datemath-parser'); - parser.parse
import * as parser from 'datemath-parser'; const timestamp = parser.parse('now+1h');const parser = require('datemath-parser'); const timestamp = parser.parse('now+1h');
Quickstart
const parser = require('datemath-parser');
// Parse a simple expression relative to 'now'
let expression1 = 'now+1h';
let timestamp1 = parser.parse(expression1);
console.log(`'${expression1}' resolves to: ${new Date(timestamp1).toISOString()} (timestamp: ${timestamp1})`);
// Parse an expression with rounding
let expression2 = 'now/d'; // round to the beginning of the day
let timestamp2 = parser.parse(expression2);
console.log(`'${expression2}' resolves to: ${new Date(timestamp2).toISOString()} (timestamp: ${timestamp2})`);
// Parse an expression with an explicit anchor date
let expression3 = '2023-01-01||+1M/d'; // January 1st, 2023 plus one month, rounded to day
let timestamp3 = parser.parse(expression3);
console.log(`'${expression3}' resolves to: ${new Date(timestamp3).toISOString()} (timestamp: ${timestamp3})`);
// Demonstrate parsing with a custom 'now' date and rounding up
const customNow = new Date('2024-03-15T10:30:00Z');
let expression4 = 'now+2d/w'; // 2 days from customNow, rounded up to the end of the week
let timestamp4 = parser.parse(expression4, customNow.getTime(), true); // Pass custom now as milliseconds, roundUp = true
console.log(`'${expression4}' (with custom now ${customNow.toISOString()}) resolves to: ${new Date(timestamp4).toISOString()} (timestamp: ${timestamp4})`);