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-relative-range

v0.16.1

Published

Calculate a date range in the past from a certain moment

Downloads

618

Readme

RelativeRange

Calculate a date range relative to a certain moment.

Build status Coverage Status

Contents

Installation

npm

npm i -S moment-relative-range

yarn

yarn add moment-relative-range

Initiation

import moment from 'moment';
import { extendMoment } from 'moment-relative-range';

extendMoment(moment);

Basic usage

Previous

var range = moment().previous(5, 'days');

// range.start = 6 days ago
// range.end = yesterday
// range.length = 5

var previousMonth = range.previous(1, 'months');

// The new range will be relative to the old one
// previousMonth.start = start of 1 months ago
// previousMonth.end = end of last month
// previousMonth.length = the length of the last month in days

var previousYear = previousMonth.previous(1, 'year');

// previousYear.start = start of the year before the previous month
// etc.

Next

var range = moment().next(2, 'month');

// range.start = 1st day of next month
// range.end = last day of the month after the next

Current

You can use moment().current(measure):

var thisMonth = moment().current('month');

// thisMonth.start = start of the month
// thisMonth.end = today
// thisMonth.length = the number of days since the start of this month

Combinations

moment()
    .previous(1, 'year') // Last year
    .current('month') // Last years December
    .previous(1, 'month') // Last years November
    .next(1, 'week'); // First week of December last year

Custom

It's also possible to construct a range yourself:

import RelativeRange from 'moment-relative-range';

var range = new RelativeRange({
    date: new Date(),
    units: 5,
    measure: 'days'
});

// The results are the same as above

Options

  • date (Date): The date to calculate the range from. required
  • measure (String): Things like month, year, day, isoWeek. required
  • units (Number): The amount of measures. required
  • margin (Number): A gap between the the date and the end date of the range, in number of days. optional
  • fixedStart (Date): A fixed start date. optional

Formatting

#format

moment().current('month').format('ll'); // 'Jan 1 - 31, 2000'

Default format is ll. There are two custom formats supported: r and R.

  • r: today, yesterday, last month, coming week, etc.
  • R: this day, previous 1 day, previous month, next week, etc.

#locale

You can set the locale of a range by using the #locale function. This works the same as for a normal moment.

moment().current('month').locale('en').format(); // 'Jan 1 - 31, 2000'
moment().current('month').locale('nl').format(); // '1 - 31 jan., 2000'

#toArray

moment().current('month').toArray(); // ['YYYY-MM-DD', 'YYYY-MM-DD']

toArray takes an optional format parameter. Defaults to YYYY-MM-DD;

Integrations

moment.range

There is a great package called moment-range, which works great with this package:

import { extendMoment as extendWithRange } from 'moment-range';

extendWithRange(moment);

const last5DaysRange = moment().previous(5, 'days');

const momentRange = moment.range(last5DaysRange.toArray());

const last5Days = momentRange.by('day'); // [day1, day2, day3, day4, day5]

const relativeRange = new RelativeRange(momentRange);

relativeRange.current('month'); // etc.