kevbuk:moment-range

v2.2.2Published 8 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Moment-range v1.0.1 for Meteor

Installation

meteor add kevb:moment-range

Usage

moment().range(start, end);

moment-range

Fancy date ranges for Moment.js.

Examples

Create

Create a date range:

1var start = new Date(2012, 0, 15)
2  , end   = new Date(2012, 4, 23)
3  , range = moment().range(start, end);

You can also create a date range with moment objects:

1var start = moment("2011-04-15", "YYYY-MM-DD")
2  , end   = moment("2011-11-27", "YYYY-MM-DD")
3  , range = moment().range(start, end);

Contains / Within / Overlaps / Intersect / Subtract

Check to see if your range contains a date/moment:

1var start  = new Date(2012, 4, 1)
2  , end    = new Date(2012, 4, 23)
3  , lol    = new Date(2012, 4, 15)
4  , wat    = new Date(2012, 2, 27)
5  , range  = moment().range(start, end)
6  , range2 = moment().range(lol, wat);
7
8range.contains(lol); // true
9range.contains(wat); // false

Find out if your moment falls within a date range:

1var start = new Date(2012, 4, 1)
2  , end   = new Date(2012, 4, 23)
3  , when  = moment("2012-05-10", "YYYY-MM-DD")
4  , range = moment().range(start, end);
5
6when.within(range); // true

Does it overlap another range?

1range.overlaps(range2); // true

What are the intersecting ranges?

1range.intersect(range2); // [moment().range(lol, end)]

Subtracting one range from another.

1range.subtract(range2); // [moment().range(start, lol)]

Iterate

Iterate over your date range by an amount of time or another range:

1var start = new Date(2012, 2, 1)
2  , two   = new Date(2012, 2, 2)
3  , end   = new Date(2012, 2, 5)
4  , range1 = moment().range(start, end)
5  , range2 = moment().range(start, two) // One day
6  , acc = [];
7
8range1.by('days', function(moment) {
9  // Do something with `moment`
10});

Any of the units accepted by moment.js' add method may be used.

You can also iterate by another range:

1range1.by(range2, function(moment) {
2  // Do something with `moment`
3  acc.push(moment);
4});
5
6acc.length == 5 // true

Compare

Compare range lengths or add them together with simple math:

1var r_1 = moment().range(new Date(2011, 2, 5), new Date(2011, 3, 15))
2  , r_2 = moment().range(new Date(1995, 0, 1), new Date(1995, 12, 25));
3
4r_2 > r_1 // true
5
6r_1 + r_2 // duration of both ranges in milliseconds
7
8Math.abs(r_1 - r_2); // difference of ranges in milliseconds

Equality

Check if two ranges are the same, i.e. their starts and ends are the same:

1var r_1 = moment().range(new Date(2011, 2, 5), new Date(2011, 3, 15))
2  , r_2 = moment().range(new Date(2011, 2, 5), new Date(2011, 3, 15))
3  , r_3 = moment().range(new Date(2011, 3, 5), new Date(2011, 6, 15));
4
5r_1.isSame(r_2) // true
6r_2.isSame(r_3) // false

Installation

moment-range works in both the browser and node.js.

Browser

Simply include moment-range after moment.js:

1<script src="/javascripts/moment-range.js"></script>

NPM

Install via npm:

npm install moment-range

Or put it in your package.json:

1{ "moment-range": "~1" }

Bower

bower install moment-range

Running Tests

Clone this bad boy:

git clone https://git@github.com/gf3/moment-range.git

Install the dependencies:

npm install

Do all the things! (including the tests)

$ grunt

License

moment-range is UNLICENSED.