PHPackages                             intelogie/moment-range - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. intelogie/moment-range

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

intelogie/moment-range
======================

0135

Since Dec 8Compare

[ Source](https://github.com/INTELOGIE/moment-range)[ Packagist](https://packagist.org/packages/intelogie/moment-range)[ RSS](/packages/intelogie-moment-range/feed)WikiDiscussions Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

moment-range
============

[](#moment-range)

Fancy date ranges for [Moment.js](http://momentjs.com/).

Detailed API documentation can be found at:

- [Examples](#examples)
    - [Create](#create)
    - [Contains / Within / Overlaps / Intersect / Add / Subtract](#contains--within--overlaps--intersect--add--subtract)
    - [Iterate](#iterate)
    - [Compare](#compare)
    - [Equality](#equality)
    - [Difference](#difference)
    - [Conversion](#conversion)
        - [`toArray`](#toarray)
        - [`toDate`](#todate)
        - [`toString`](#tostring)
        - [`valueOf`](#valueof)
    - [Center](#center)
    - [Clone](#clone)
- [Installation](#installation)
    - [Browser](#browser)
    - [NPM](#npm)
    - [Bower](#bower)
- [Running Tests](#running-tests)
- [License](#license)

Examples
--------

[](#examples)

### Create

[](#create)

Create a date range:

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

You can also create a date range with moment objects:

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

Arrays work too:

```
var dates = [moment("2011-04-15", "YYYY-MM-DD"), moment("2011-11-27", "YYYY-MM-DD")];
var range = moment.range(dates);
```

You can also create a range from an [ISO 8601 time interval](http://en.wikipedia.org/wiki/ISO_8601#Time_intervals) string:

```
var timeInterval = "2015-01-17T09:50:04+00:00/2015-04-17T08:29:55+00:00";
var range = moment.range(timeInterval);
```

You can also create a range from the start until the end of a named interval:

```
var date = moment("2011-04-15", "YYYY-MM-DD");
var range = date.range("month");
```

You can also create open-ended ranges which go to the earliest or latest possible date:

```
var rangeUntil = moment.range(null, "2011-05-05");
var rangeFrom = moment.range("2011-03-05", null);
var rangeAllTime = moment.range(null, null);
```

### Contains / Within / Overlaps / Intersect / Add / Subtract

[](#contains--within--overlaps--intersect--add--subtract)

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

```
var start  = new Date(2012, 4, 1);
var end    = new Date(2012, 4, 23);
var lol    = new Date(2012, 4, 15);
var wat    = new Date(2012, 4, 27);
var range  = moment.range(start, end);
var range2 = moment.range(lol, wat);

range.contains(lol); // true
range.contains(wat); // false
```

A optional second parameter indicates if the end of the range should be excluded when testing for inclusion

```
range.contains(end) // true
range.contains(end, false) // true
range.contains(end, true) // false
```

Find out if your moment falls within a date range:

```
var start = new Date(2012, 4, 1);
var end   = new Date(2012, 4, 23);
var when  = moment("2012-05-10", "YYYY-MM-DD");
var range = moment.range(start, end);

when.within(range); // true
```

Does it overlap another range?

```
range.overlaps(range2); // true
```

What are the intersecting ranges?

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

Add/combine/merge overlapping ranges.

```
range.add(range2); // [moment.range(start, wat)]

var range3 = moment.range(new Date(2012, 3, 1), new Date(2012, 3, 15);
range.add(range3); // [null]
```

Subtracting one range from another.

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

### Iterate

[](#iterate)

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

```
var start = new Date(2012, 2, 1);
var two   = new Date(2012, 2, 2);
var end   = new Date(2012, 2, 5);
var range1 = moment.range(start, end);
var range2 = moment.range(start, two); // One day
var acc = [];

range1.by('days', function(moment) {
  // Do something with `moment`
});
```

Any of the units accepted by [moment.js' `add`method](http://momentjs.com/docs/#/manipulating/add/) may be used.

You can also iterate by another range:

```
range1.by(range2, function(moment) {
  // Do something with `moment`
  acc.push(moment);
});

acc.length == 5 // true
```

Iteration also supports excluding the end value of the range by setting the last parameter to `true`.

```
var acc = [];

range1.by('d', function (moment) {
  acc.push(moment)
}, true);

acc.length == 4 // true
```

### Compare

[](#compare)

Compare range lengths or add them together with simple math:

```
var r_1 = moment.range(new Date(2011, 2, 5), new Date(2011, 3, 15));
var r_2 = moment.range(new Date(1995, 0, 1), new Date(1995, 12, 25));

r_2 > r_1 // true

r_1 + r_2 // duration of both ranges in milliseconds

Math.abs(r_1 - r_2); // difference of ranges in milliseconds
```

### Equality

[](#equality)

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

```
var r_1 = moment.range(new Date(2011, 2, 5), new Date(2011, 3, 15));
var r_2 = moment.range(new Date(2011, 2, 5), new Date(2011, 3, 15));
var r_3 = moment.range(new Date(2011, 3, 5), new Date(2011, 6, 15));

r_1.isSame(r_2); // true
r_2.isSame(r_3); // false
```

### Difference

[](#difference)

The difference of the entire range given various units.

Any of the units accepted by [moment.js' `add`method](http://momentjs.com/docs/#/manipulating/add/) may be used.

```
var start = new Date(2011, 2, 5);
var end   = new Date(2011, 5, 5);
var dr    = moment.range(start, end);

dr.diff('months'); // 3
dr.diff('days'); // 92
dr.diff(); // 7945200000
```

### Conversion

[](#conversion)

#### `toArray`

[](#toarray)

Converts the `DateRange` to an `Array` of `Date` objects.

```
var start = new Date(2011, 2, 5);
var end   = new Date(2011, 5, 5);
var dr    = moment.range(start, end);

dr.toArray('days'); // [new Date(2011, 2, 5), new Date(2011, 3, 5), new Date(2011, 4, 5), new Date(2011, 5, 5)]
```

#### `toDate`

[](#todate)

Converts the `DateRange` to an `Array` of the start and end `Date` objects.

```
var start = new Date(2011, 2, 5);
var end   = new Date(2011, 5, 5);
var dr    = moment.range(start, end);

dr.toDate(); // [new Date(2011, 2, 5), new Date(2011, 5, 5)]
```

#### `toString`

[](#tostring)

Converting a `DateRange` to a `String` will format it as an [ISO 8601 time interval](http://en.wikipedia.org/wiki/ISO_8601#Time_intervals):

```
var start = '2015-01-17T09:50:04+00:00';
var end   = '2015-04-17T08:29:55+00:00';
var range = moment.range(moment.utc(start), moment.utc(end));

range.toString() // "2015-01-17T09:50:04+00:00/2015-04-17T08:29:55+00:00"
```

#### `valueOf`

[](#valueof)

The difference between the end date and start date in milliseconds.

```
var start = new Date(2011, 2, 5);
var end   = new Date(2011, 5, 5);
var range = moment.range(start, end);

range.valueOf(); // 7945200000
```

### Center

[](#center)

Calculate the center of a range

```
var start = new Date(2011, 2, 5);
var end   = new Date(2011, 3, 5);
var dr    = moment.range(start, end);

dr.center(); // 1300622400000
```

### Clone

[](#clone)

Deep clone a range

```
var start = new Date(2011, 2, 5);
var end   = new Date(2011, 3, 5);
var dr    = moment.range(start, end);

var dr2 = dr.clone();
dr2.start.add(2, 'days');

dr2.start.toDate() === dr.start.toDate() // false
```

Installation
------------

[](#installation)

moment-range works in both the browser and [node.js](http://nodejs.org/).

### Node / NPM

[](#node--npm)

Install via npm:

```
npm install moment-range
```

And then `require` it:

```
var moment = require('moment');
require('moment-range');
```

### Browser

[](#browser)

Simply include moment-range after moment.js:

```

```

Thanks to the fine people at [cdnjs](https://github.com/cdnjs/cdnjs), you can link to moment-range from the [cdnjs servers](https://cdnjs.com/libraries/moment-range).

### Bower

[](#bower)

```
bower install moment-range
```

**Note:** Include `moment-range` *after* `moment`.

Running Tests
-------------

[](#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!

```
npm run-script build
npm run-script test
npm run-script jsdoc
```

License
-------

[](#license)

moment-range is [UNLICENSED](http://unlicense.org/).

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 73.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/8cd960536c07e255b2e6f7e55c03c6d09274b215ccceaccc38926ffe03e098a7?d=identicon)[bgauthier](/maintainers/bgauthier)

---

Top Contributors

[![gf3](https://avatars.githubusercontent.com/u/18397?v=4)](https://github.com/gf3 "gf3 (105 commits)")[![fidothe](https://avatars.githubusercontent.com/u/8550?v=4)](https://github.com/fidothe "fidothe (5 commits)")[![wilgert](https://avatars.githubusercontent.com/u/503813?v=4)](https://github.com/wilgert "wilgert (4 commits)")[![stuartleigh](https://avatars.githubusercontent.com/u/414998?v=4)](https://github.com/stuartleigh "stuartleigh (4 commits)")[![pronebel](https://avatars.githubusercontent.com/u/911730?v=4)](https://github.com/pronebel "pronebel (3 commits)")[![jdforsythe](https://avatars.githubusercontent.com/u/286972?v=4)](https://github.com/jdforsythe "jdforsythe (3 commits)")[![nd0ut](https://avatars.githubusercontent.com/u/670959?v=4)](https://github.com/nd0ut "nd0ut (3 commits)")[![tb](https://avatars.githubusercontent.com/u/71683?v=4)](https://github.com/tb "tb (3 commits)")[![thomasvanlankveld](https://avatars.githubusercontent.com/u/5240626?v=4)](https://github.com/thomasvanlankveld "thomasvanlankveld (2 commits)")[![scotthovestadt](https://avatars.githubusercontent.com/u/1831484?v=4)](https://github.com/scotthovestadt "scotthovestadt (2 commits)")[![rosskevin](https://avatars.githubusercontent.com/u/136564?v=4)](https://github.com/rosskevin "rosskevin (2 commits)")[![teamon](https://avatars.githubusercontent.com/u/8083?v=4)](https://github.com/teamon "teamon (1 commits)")[![jkimbo](https://avatars.githubusercontent.com/u/691952?v=4)](https://github.com/jkimbo "jkimbo (1 commits)")[![aristide-n](https://avatars.githubusercontent.com/u/2230766?v=4)](https://github.com/aristide-n "aristide-n (1 commits)")[![twalpole](https://avatars.githubusercontent.com/u/16556?v=4)](https://github.com/twalpole "twalpole (1 commits)")[![bradleyayers](https://avatars.githubusercontent.com/u/105820?v=4)](https://github.com/bradleyayers "bradleyayers (1 commits)")[![bgauthier](https://avatars.githubusercontent.com/u/1789355?v=4)](https://github.com/bgauthier "bgauthier (1 commits)")

### Embed Badge

![Health badge](/badges/intelogie-moment-range/health.svg)

```
[![Health](https://phpackages.com/badges/intelogie-moment-range/health.svg)](https://phpackages.com/packages/intelogie-moment-range)
```

###  Alternatives

[ardagnsrn/ollama-php

This is a PHP library for Ollama. Ollama is an open-source project that serves as a powerful and user-friendly platform for running LLMs on your local machine. It acts as a bridge between the complexities of LLM technology and the desire for an accessible and customizable AI experience.

21074.5k](/packages/ardagnsrn-ollama-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
