npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

moment-date-math

v0.1.3

Published

This is a plugin to the Moment.js library to support date-math expression as a string to the moment object

Downloads

153

Readme

moment-date-math

moment plugin to parse a date math, elasticsearch style

This is a plugin to the Moment.js library to support date-math operations to the moment object. Date maths operations are inputted as a string whose format is based on elasticsearch date-math format (see https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math).

This plugin does not have any dependencies beyond Moment.js itself, and may be used in the browser and in Node.js.

English is not my mother-tongue, so please forgive any mistakes and the incomprehensive readme. Any help with this documentation or source code is appreciated and welcomed


Installation

Node.js

npm install moment-date-math

Bower and other package managers

I haven't tested yet, but it should be similar to npm installation

Browser <script src="path/to/moment-date-math.js"></script>

When using this plugin in the browser, be sure to include moment.js on your page first.


Usage

Module

To use this plugin as a module, use the require function:

require("moment-date-math");

Or replace your existing

var moment = require("moment");

by

var moment = require("moment-date-math");

Example: (run at time zone +07:00)

// the following 2 lines are equivalent:
moment.parseMath('2016-06-12 21:00:00Z||+1y2M3w-4d5h6m7s').format()
moment('2016-06-12 21:00:00Z').parseMath('+1y2M3w-4d5h6m7s').format()
// => moment('2016-06-12 21:00:00Z').add(1, 'y').add(2, 'M').add(3, 'w').subtract(4, 'd').subtract(5, 'h').subtract(6, 'm').subtract(7, 's').format()
// => 2017-08-29T22:53:53+07:00
moment.parseMath('May 6th 2016 10:23:23[Z]+ 1M2d /d - 3h 4m', 'MMM Do Y hh:mm:ss').format()
// => moment('May 6th 2016 10:23:23', 'MMM Do Y hh:mm:ss').utcOffset('Z').add(1, 'M').add(2, 'd').startOf('day').subtract(3, 'h').subtract(4, 'm').format()
// => 2016-06-07T20:56:00Z
// the following 2 lines are equivalent:
moment.parseMath('now[+05:00]-28h/d + 15d/M').format()
moment().calc('[+05:00]-28h/d + 15d/M').format()
// => moment().utcOffset('+05:00').subtract(28, 'h').startOf('day').add(15, 'd').startOf('month')
// => 2016-06-01T00:00:00+05:00
moment.parseMath('2016-06-12 13:00:00 +05:00 [@]- 28 h /d + 15 d /M').format()
// => moment('2016-06-12 13:00:00 +05:00').utcOffset('2016-06-12 20:00:00 +05:00').subtract(28, 'h').startOf('day').add(15, 'd').startOf('month')
// => 2016-06-01T00:00:00+05:00

Usage

(Convention: I use x? for an optional parameter x)

moment.parseMath(fullExpression, format?): create a moment from a dateMath expression and format

moment().calc(mathExpression): apply a dateMath expression to existing moment

where:

fullExpression = < datePart > + < splitter? > + < mathExpression >

  • A splitter, if exists, must be '||'
  • moment.parseMath(fullExpression, format?) is equivalent to moment(datePart, format?).calc(mathExpression), except for:
    • the [@] operator, see below
    • if < datePart > is 'now' or '' (empty string), in which case it is equivalent to moment().calc(mathExpression)

A mathExpression is a string composed by consecutive dateMath operators, with optional spaces between them. There are 4 type of operators:

  • Set utcOffset operator: [@] or [Z] or [+XX:XX] or [-XX:XX], where each 'X' is a digit. It translates to the function call .utcOffset(<whatever between the square brackets>), with the exception of [@]
    • There should be at most 1 utcOffset operator, and if exists, it should be the first operator in the series
    • [@] should only be used in moment.parseMath, where the contains a utc offset suffix, e.g: Z or +XX:XX. It translates to .utcOffset(< datePart >);
  • Add operator: has the format of +< number1 >< unit1 >< number2 >< unit2 >...< number_n >< unit_n >. Add operator is space tolerant, i.e there can be whitespaces in between
    • The numbers are non-negative integers. Each unit is a character from the set [yMdhms]
    • It translates to .add(< number1 >, < unit1 >).add(< number2 >, < unit2 >)....add(< number_n >, < unit_n >)
  • Subtract operator: similar to add operator but starts with a minus (-< number1 >< unit1 >< number2 >< unit2 >...< number_n >< unit_n >)
  • startOf operator: has the format of /[yMdhms]
    • Consecutive startOf operators are not allowed (and it doesn't make sense)
    • It tranlates to .startOf(< fullUnitNotation >) where < fullUnitNotation > is 'year', 'month', 'day', 'hour', 'minute', 'second' respectively

I know, I'm not good at writing documents, nor at English. Help is appreciated. Hope you get the basic idea what this plugin is for, at least from the examples.