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 🙏

© 2024 – Pkg Stats / Ryan Hefner

moment-relativism

v1.0.0

Published

Parse grafana-inspired relative date notation into Momentjs.

Downloads

8

Readme

moment-relativism

Build Status

Parse a grafana-inspired relative date notation into momentjs instances. Where "relative" means: relative to now.

Usage

Pass in a string in moment-relativism's relative date notation.


var moment = require('moment-relativism');
var result = moment.relativism('now-7d');

// logs "Thu Aug 04 2016 16:03:28 GMT+0200" when now is "Thu Aug 11 2016 16:03:28 GMT+0200"
console.log(result.toString());

You can also pass in an object. moment.relativism will return an object of the same structure (same fields), but with the values replaced with their current momentjs equivalent instances.


var moment = require('moment-relativism');

// I want the date range for exactly the last 7 days (not rounded).
var range = {
    from: 'now-7d',
    to: 'now'
};

var result = moment.relativism(range);

// logs "Thu Aug 04 2016 16:03:28 GMT+0200" and "Thu Aug 11 2016 16:03:28 GMT+0200"
console.log(result.from.toString(), result.to.toString());

Check out the tests directory for more detailed usage.

Notation

moment-relativism's notation is based on grafana's relative date parser. However, grafana's notation does not provide a way to distinguish between rounding up or rounding down. Instead the rounding character / is rounded up or down based on the context the string is used in. If the string is used as a "from" date in a date range, it rounds down. Otherwise it rounds down.

moment-relativism differs here, as it adds a | character for rounding down. / is always used for rounding up.

When adding, subtracting and rounding you can use moment's shorthand keys for years, days, etc as found in moment's documentation.

Notation | Result ---------- | ------ now+1d | Add one day to now now-1d | Subtract one day from now now+1d/d | Add one day to now, round up to end of day now-1d|d | Subtract one day from now, round down to start of day now | Just returns the date corresponding to now now/d | Rounds up to todays end of day. now|d | Rounds down to todays start of day.

License

This software is copyrighted by Advanced Climate Systems bv, and released under the Mozilla Public License v2.0. See the LICENSE file for the entire license.