{"id":14523,"library":"datemath-parser","title":"Elasticsearch Date Math Parser","description":"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.","status":"abandoned","version":"1.0.6","language":"javascript","source_language":"en","source_url":"https://github.com/randing89/datemath-parser","tags":["javascript"],"install":[{"cmd":"npm install datemath-parser","lang":"bash","label":"npm"},{"cmd":"yarn add datemath-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add datemath-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CommonJS-only and does not provide native ESM exports. Using `import` will lead to runtime errors in pure ESM environments without a CommonJS wrapper or bundler.","wrong":"import parser from 'datemath-parser';","symbol":"parser","correct":"const parser = require('datemath-parser');"},{"note":"The `parse` method is exposed directly on the module's default export object. Destructuring in CommonJS is a common pattern to directly access it.","wrong":"import { parse } from 'datemath-parser';","symbol":"parse","correct":"const { parse } = require('datemath-parser');"},{"note":"Although `import * as` might work in some transpiled environments, it's not the native or intended way for this CommonJS-only library, which typically exports a single object or function.","wrong":"import * as parser from 'datemath-parser';\nconst timestamp = parser.parse('now+1h');","symbol":"parser.parse","correct":"const parser = require('datemath-parser');\nconst timestamp = parser.parse('now+1h');"}],"quickstart":{"code":"const parser = require('datemath-parser');\n\n// Parse a simple expression relative to 'now'\nlet expression1 = 'now+1h';\nlet timestamp1 = parser.parse(expression1);\nconsole.log(`'${expression1}' resolves to: ${new Date(timestamp1).toISOString()} (timestamp: ${timestamp1})`);\n\n// Parse an expression with rounding\nlet expression2 = 'now/d'; // round to the beginning of the day\nlet timestamp2 = parser.parse(expression2);\nconsole.log(`'${expression2}' resolves to: ${new Date(timestamp2).toISOString()} (timestamp: ${timestamp2})`);\n\n// Parse an expression with an explicit anchor date\nlet expression3 = '2023-01-01||+1M/d'; // January 1st, 2023 plus one month, rounded to day\nlet timestamp3 = parser.parse(expression3);\nconsole.log(`'${expression3}' resolves to: ${new Date(timestamp3).toISOString()} (timestamp: ${timestamp3})`);\n\n// Demonstrate parsing with a custom 'now' date and rounding up\nconst customNow = new Date('2024-03-15T10:30:00Z');\nlet expression4 = 'now+2d/w'; // 2 days from customNow, rounded up to the end of the week\nlet timestamp4 = parser.parse(expression4, customNow.getTime(), true); // Pass custom now as milliseconds, roundUp = true\nconsole.log(`'${expression4}' (with custom now ${customNow.toISOString()}) resolves to: ${new Date(timestamp4).toISOString()} (timestamp: ${timestamp4})`);","lang":"javascript","description":"This quickstart demonstrates parsing various Elasticsearch-compatible date math expressions, including relative to 'now', with rounding, and using an explicit anchor date. It also shows how to provide a custom 'now' value and control rounding behavior."},"warnings":[{"fix":"For new projects, consider actively maintained alternatives like `@elastic/datemath` (if compatible with your needs) or general-purpose date libraries such as `date-fns` or `Moment.js` which offer similar parsing capabilities and ongoing support.","message":"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.","severity":"gotcha","affected_versions":">=1.0.6"},{"fix":"Ensure your project or file uses CommonJS (`require()`) to import the library, or use a bundler (like Webpack, Rollup, Parcel) that can handle CommonJS modules in an ESM context. If migrating to ESM, consider replacing this library with an ESM-compatible alternative.","message":"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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"You will need to create a declaration file (`datemath-parser.d.ts`) manually in your project to provide types for the `parse` function, or rely on `@ts-ignore` for type-unsafe usage. Consider alternatives that offer native TypeScript support for a better development experience.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement to use CommonJS `require()`: `const parser = require('datemath-parser');` or `const { parse } = require('datemath-parser');`","cause":"Attempting to import `datemath-parser` using an `import` statement in an ECMAScript Module (ESM) environment.","error":"TypeError: require is not a function"},{"fix":"Ensure 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.","cause":"The `datemath-parser` module or its `parse` function was not correctly imported or required before being used.","error":"ReferenceError: parser is not defined"},{"fix":"Review 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 (`+`, `-`, `/`).","cause":"The input string provided to `parser.parse()` does not conform to the expected Elasticsearch date math format.","error":"Error: Invalid Date Math String"}],"ecosystem":"npm"}