From 33f79577a16f56629ddf9c1f1034259da7222d9c Mon Sep 17 00:00:00 2001 From: Take Weiland Date: Sat, 4 Mar 2023 04:16:04 +0100 Subject: [PATCH] Fix Interval#count counting the endpoint as part of the interval (#1308) * Fix Interval#count counting the endpoint as part of the interval Fixes #1306 * Run formatter --- src/interval.js | 2 +- test/interval/info.test.js | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/interval.js b/src/interval.js index a4c177ae6..33caf6d4d 100644 --- a/src/interval.js +++ b/src/interval.js @@ -240,7 +240,7 @@ export default class Interval { if (!this.isValid) return NaN; const start = this.start.startOf(unit), end = this.end.startOf(unit); - return Math.floor(end.diff(start, unit).get(unit)) + 1; + return Math.floor(end.diff(start, unit).get(unit)) + (end.valueOf() !== this.end.valueOf()); } /** diff --git a/test/interval/info.test.js b/test/interval/info.test.js index 466f0961c..9be5510a8 100644 --- a/test/interval/info.test.js +++ b/test/interval/info.test.js @@ -54,9 +54,14 @@ test("Interval#count('years') returns 2 if the interval crosses the new year", ( expect(i.count("years")).toBe(2); }); +test("Interval#count() does not count the endpoint of the interval", () => { + const i = DateTime.fromISO("2022-10-01").until(DateTime.fromISO("2022-10-03")); + expect(i.count("days")).toBe(2); +}); + test("Interval#count() uses milliseconds by default", () => { const i = DateTime.fromISO("2016-05-25T03:00").until(DateTime.fromISO("2016-05-25T14:00")); - expect(i.count()).toBe(39600001); + expect(i.count()).toBe(39600000); }); test("Interval#count() returns NaN for invalid intervals", () => {