# moment-relativism

> Parse grafana-inspired relative date notation into Momentjs.

Latest version **1.0.0** (published 2016-08-15) · MPL-2.0 license · 0 weekly downloads

## Install

```sh
npm install moment-relativism
pnpm add moment-relativism
yarn add moment-relativism
bun add moment-relativism
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2016-08-15 |
| First published | 2016-08-15 |
| Weekly downloads | 0 |
| License | MPL-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | broekhuizen@baopt.nl |
| Maintainers | jaapz |
| Keywords | grafana, moment, relative |

## Links

- npm: https://www.npmjs.com/package/moment-relativism
- Repository: https://github.com/AdvancedClimateSystems/moment-relativism
- Homepage: https://github.com/AdvancedClimateSystems/moment-relativism#readme
- Issues: https://github.com/AdvancedClimateSystems/moment-relativism/issues
- npm.io page: https://npm.io/package/moment-relativism

## Dependencies (1)

- [moment](https://npm.io/package/moment.md) ^2.10.2

## Recent versions

- 1.0.0 (latest) — 2016-08-15

## README

moment-relativism
=================

[![Build Status](https://travis-ci.org/AdvancedClimateSystems/moment-relativism.svg?branch=master)](https://travis-ci.org/AdvancedClimateSystems/moment-relativism)

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.

```javascript

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.

```javascript

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](https://github.com/grafana/grafana/blob/56622ee2c6d030efb7b39433b1b1fc6f0e7250a4/public/app/core/utils/rangeutil.ts).
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](http://momentjs.com/docs/#/manipulating/add/).


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.

---
_Source: https://npm.io/package/moment-relativism · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
