Skip to content

Commit 5aa55da

Browse files
committed
Improve documentation regarding Interval's half-openness
Fixes #1710
1 parent b188e10 commit 5aa55da

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

docs/tour.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,31 @@ i.toISO() //=> '2017-09-14T04:07:11.532-04:00/2020-10-12T00:00:00.000-04:0
216216
i.toString() //=> '[2017-09-14T04:07:11.532-04:00 – 2020-10-12T00:00:00.000-04:00)
217217
```
218218

219+
Note that Luxon's Intervals are always half-open, meaning the starting point is included in the interval while the end point is not.
220+
The following code will not work as expected:
221+
```js
222+
const start = DateTime.now().startOf('day');
223+
const end = start.endOf('day');
224+
const i = Interval.fromDateTimes(start, end);
225+
226+
i.length('hours') //=> 23.99999972222222
227+
i.toString() //=> [2025-07-09T00:00:00.000+02:00 – 2025-07-09T23:59:59.999+02:00)
228+
```
229+
230+
This is because `endOf('day')` returns the last millisecond of the day and as a result that last millisecond is _not_
231+
included in the interval.
232+
233+
`Interval.after` or `Interval.before` are better suited for creating Intervals of a specific length:
234+
```js
235+
const start = DateTime.now().startOf('day');
236+
const i1 = Interval.after(start, { days: 1 });
237+
const i2 = Interval.before(start, { days: 1 });
238+
239+
i1.length('hours') //=> 24
240+
i1.toString() //=> [2025-07-09T00:00:00.000+02:00 – 2025-07-10T00:00:00.000+02:00)
241+
242+
i2.length('hours') //=> 24
243+
i2.toString() //=> [2025-07-08T00:00:00.000+02:00 – 2025-07-09T00:00:00.000+02:00)
244+
```
245+
219246
Intervals can be split up into smaller intervals, perform set-like operations with other intervals, and few other handy features. See the `Interval` API docs.

src/interval.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ export default class Interval {
189189
}
190190

191191
/**
192-
* Returns the end of the Interval
192+
* Returns the end of the Interval. This is the first instant which is not part of the interval
193+
* (Interval is half-open).
193194
* @type {DateTime}
194195
*/
195196
get end() {

0 commit comments

Comments
 (0)