Calculate a date range in the past from a certain moment
npm i -S previous-date-range
yarn add previous-date-range
You can use moment().previous(units, measure)
:
import moment from 'moment';
import 'previous-date-range';
var range = moment().previous(5, 'days');
// range.start = 6 days ago
// range.end = yesterday
// range.length = 5
var clone = range.previous(2, 'months');
// clone.start = start of 2 months ago
// clone.end = end of last month
// clone.length = the length of the last 2 months
It's also possible to construct a range yourself:
import { PreviousDateRange as Range } from 'previous-date-range';
var range = new Range({
date: new Date(),
units: 5,
measure: 'days'
});
// The results are the same as above