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.
npm install datemath-parserVerified import paths — ran on the pinned version, not inferred.
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.
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.
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.
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.
Change your import statement to use CommonJS `require()`: `const parser = require('datemath-parser');` or `const { parse } = require('datemath-parser');`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.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 (`+`, `-`, `/`).
No dependency data recorded yet.