From 561d128a02dcc6262afaef2b28f27dc3c59ffdd2 Mon Sep 17 00:00:00 2001 From: Kris Tremblay Date: Wed, 17 Feb 2021 17:10:16 -0600 Subject: [PATCH 01/16] Added businessDiff method to get number of buiness days between DateTimes. --- README.md | 13 +++++++++++++ src/index.js | 35 +++++++++++++++++++++++++++++++++++ src/index.test.js | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/README.md b/README.md index 7ad4e49..00d94b6 100644 --- a/README.md +++ b/README.md @@ -264,6 +264,7 @@ dt.isHoliday('middle-america', {some: 'stuff'}); * [.isBusinessDay()](#DateTime+isBusinessDay) ⇒ boolean * [.plusBusiness([days])](#DateTime+plusBusiness) ⇒ [DateTime](#DateTime) * [.minusBusiness([days])](#DateTime+minusBusiness) ⇒ [DateTime](#DateTime) + * [.businessDiff(targetDate, relative)](#DateTime+businessDiff) ⇒ number @@ -362,6 +363,18 @@ Subtracts business days to an existing DateTime instance. | --- | --- | --- | --- | | [days] | number | 1 | The number of business days to subtract. | + + +### dateTime.businessDiff(targetDate, relative) ⇒ number +Returns the difference in business days. Set relative to true if you need to support past dates. + +**Kind**: instance method of [DateTime](#DateTime) + +| Param | Type | +| --- | --- | +| targetDate | [DateTime](#DateTime) | +| relative | boolean | + ## getEasterMonthAndDay(year) ⇒ Array.<number> diff --git a/src/index.js b/src/index.js index 1aaf0ed..76118e0 100644 --- a/src/index.js +++ b/src/index.js @@ -146,4 +146,39 @@ DateTime.prototype.minusBusiness = function({ days = ONE_DAY } = {}) { return this.plusBusiness({ days: -days }); }; +/** + * Returns the difference in business days. Set relative to true if you need to support past dates. + * @param {DateTime} targetDate + * @param {boolean} relative + * @returns {number} + */ +DateTime.prototype.businessDiff = function(targetDate, relative = false) { + let dt = this; + let start = dt < targetDate ? dt : targetDate; + let end = dt < targetDate ? targetDate : dt; + let daysDiff = 0; + let isSameDay = + dt.hasSame(targetDate, 'day') && + dt.hasSame(targetDate, 'month') && + dt.hasSame(targetDate, 'year'); + + if (isSameDay) { + return daysDiff; + } + + while (start < end) { + if (start.isBusinessDay() && !start.isHoliday()) { + daysDiff += 1; + } + + start = start.plus({ days: 1 }); + } + + if (relative) { + return dt <= targetDate ? daysDiff : -daysDiff; + } + + return daysDiff; +}; + export { DateTime }; diff --git a/src/index.test.js b/src/index.test.js index d3d86ad..e940131 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -324,3 +324,39 @@ describe('time zone is carried over after a business-day operation', () => { expect(nyPlusTwo.zoneName).toBe('America/New_York'); }); }); + +describe('business day diff', () => { + it('knows two identical DateTimes have a business day diff of 0', () => { + const targetDate = DateTime.local().plus({ hours: 2 }); + + expect(DateTime.local().businessDiff(targetDate)).toEqual(0); + }); + + it('knows there are three business days between two dates that are 3 business days apart', () => { + const myCompanyTakesNoHolidays = []; + const startDate = DateTime.local(); + const futureDate = DateTime.local().plusBusiness({ days: 3 }); + const pastDate = DateTime.local().minusBusiness({ days: 3 }); + + startDate.setupBusiness({ + holidayMatchers: myCompanyTakesNoHolidays, + }); + + expect(startDate.businessDiff(futureDate)).toEqual(3); + expect(startDate.businessDiff(pastDate)).toEqual(3); + }); + + it('knows diff is negative for the past and positive for the future if relative is specified', () => { + const myCompanyTakesNoHolidays = []; + const startDate = DateTime.local(); + const futureDate = DateTime.local().plusBusiness({ days: 3 }); + const pastDate = DateTime.local().minusBusiness({ days: 3 }); + + startDate.setupBusiness({ + holidayMatchers: myCompanyTakesNoHolidays, + }); + + expect(startDate.businessDiff(futureDate, true)).toEqual(3); + expect(startDate.businessDiff(pastDate, true)).toEqual(-3); + }); +}); From f49c59baf85c6b7c8ecb5a09c051d22a792b9d40 Mon Sep 17 00:00:00 2001 From: Kris Tremblay Date: Wed, 17 Feb 2021 17:28:12 -0600 Subject: [PATCH 02/16] Fixed failing test due to time issue. In practice this would be handled based on developer needs. --- src/index.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/index.test.js b/src/index.test.js index e940131..b4cb9da 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -327,7 +327,9 @@ describe('time zone is carried over after a business-day operation', () => { describe('business day diff', () => { it('knows two identical DateTimes have a business day diff of 0', () => { - const targetDate = DateTime.local().plus({ hours: 2 }); + const targetDate = DateTime.fromMillis(new Date().setHours(0, 0)) + .setLocale('America/New_York') + .plus({ hours: 2 }); expect(DateTime.local().businessDiff(targetDate)).toEqual(0); }); From 11c3b0502393a9788fb3b86b1054bec2b51f4bd0 Mon Sep 17 00:00:00 2001 From: Kris Tremblay Date: Sat, 20 Feb 2021 13:51:47 -0600 Subject: [PATCH 03/16] Clean tests, fixed bug in while to incl. last day. --- src/index.js | 2 +- src/index.test.js | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/index.js b/src/index.js index 76118e0..e8ddfe8 100644 --- a/src/index.js +++ b/src/index.js @@ -166,7 +166,7 @@ DateTime.prototype.businessDiff = function(targetDate, relative = false) { return daysDiff; } - while (start < end) { + while (start <= end) { if (start.isBusinessDay() && !start.isHoliday()) { daysDiff += 1; } diff --git a/src/index.test.js b/src/index.test.js index b4cb9da..4d7b9a9 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -327,8 +327,8 @@ describe('time zone is carried over after a business-day operation', () => { describe('business day diff', () => { it('knows two identical DateTimes have a business day diff of 0', () => { - const targetDate = DateTime.fromMillis(new Date().setHours(0, 0)) - .setLocale('America/New_York') + const targetDate = DateTime.local() + .startOf('day') .plus({ hours: 2 }); expect(DateTime.local().businessDiff(targetDate)).toEqual(0); @@ -336,9 +336,9 @@ describe('business day diff', () => { it('knows there are three business days between two dates that are 3 business days apart', () => { const myCompanyTakesNoHolidays = []; - const startDate = DateTime.local(); - const futureDate = DateTime.local().plusBusiness({ days: 3 }); - const pastDate = DateTime.local().minusBusiness({ days: 3 }); + const startDate = DateTime.local().startOf('day'); + const futureDate = startDate.plusBusiness({ days: 3 }); + const pastDate = startDate.minusBusiness({ days: 3 }); startDate.setupBusiness({ holidayMatchers: myCompanyTakesNoHolidays, @@ -350,9 +350,9 @@ describe('business day diff', () => { it('knows diff is negative for the past and positive for the future if relative is specified', () => { const myCompanyTakesNoHolidays = []; - const startDate = DateTime.local(); - const futureDate = DateTime.local().plusBusiness({ days: 3 }); - const pastDate = DateTime.local().minusBusiness({ days: 3 }); + const startDate = DateTime.local().startOf('day'); + const futureDate = startDate.plusBusiness({ days: 3 }); + const pastDate = startDate.minusBusiness({ days: 3 }); startDate.setupBusiness({ holidayMatchers: myCompanyTakesNoHolidays, From 6a81a3476e56aede2162e46a5b9e4c5170327ce7 Mon Sep 17 00:00:00 2001 From: Kris Tremblay Date: Wed, 24 Feb 2021 14:58:42 -0600 Subject: [PATCH 04/16] Reverted < to <= in day count loop. --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index e8ddfe8..76118e0 100644 --- a/src/index.js +++ b/src/index.js @@ -166,7 +166,7 @@ DateTime.prototype.businessDiff = function(targetDate, relative = false) { return daysDiff; } - while (start <= end) { + while (start < end) { if (start.isBusinessDay() && !start.isHoliday()) { daysDiff += 1; } From 34e822650f24c79471cf75dbb2f67bf2ca6afcaa Mon Sep 17 00:00:00 2001 From: Kris Tremblay Date: Wed, 3 Mar 2021 12:11:04 -0600 Subject: [PATCH 05/16] Replaced relative param with config object added includeEndDate --- README.md | 40 +++++++++++++++++++++++++++------------- src/index.js | 12 +++++++++--- src/index.test.js | 24 +++++++++++++++++++++--- 3 files changed, 57 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 00d94b6..7f93520 100644 --- a/README.md +++ b/README.md @@ -249,6 +249,14 @@ dt.isHoliday('middle-america', {some: 'stuff'}); +## Typedefs + +
+
BusinessDiffConfignumber
+

Returns the difference in business days. Set relative to true if you need to support past dates.

+
+
+ ## DateTime ⇐ [DateTime](#DateTime) @@ -264,7 +272,6 @@ dt.isHoliday('middle-america', {some: 'stuff'}); * [.isBusinessDay()](#DateTime+isBusinessDay) ⇒ boolean * [.plusBusiness([days])](#DateTime+plusBusiness) ⇒ [DateTime](#DateTime) * [.minusBusiness([days])](#DateTime+minusBusiness) ⇒ [DateTime](#DateTime) - * [.businessDiff(targetDate, relative)](#DateTime+businessDiff) ⇒ number @@ -363,18 +370,6 @@ Subtracts business days to an existing DateTime instance. | --- | --- | --- | --- | | [days] | number | 1 | The number of business days to subtract. | - - -### dateTime.businessDiff(targetDate, relative) ⇒ number -Returns the difference in business days. Set relative to true if you need to support past dates. - -**Kind**: instance method of [DateTime](#DateTime) - -| Param | Type | -| --- | --- | -| targetDate | [DateTime](#DateTime) | -| relative | boolean | - ## getEasterMonthAndDay(year) ⇒ Array.<number> @@ -387,3 +382,22 @@ Returns the month and day of Easter for a given year. | --- | --- | | year | number | + + +## BusinessDiffConfig ⇒ number +Returns the difference in business days. Set relative to true if you need to support past dates. + +**Kind**: global typedef + +| Param | Type | +| --- | --- | +| targetDate | [DateTime](#DateTime) | +| config | [BusinessDiffConfig](#BusinessDiffConfig) | + +**Properties** + +| Name | Type | Default | Description | +| --- | --- | --- | --- | +| [includeEndDate] | boolean | false | includeEndDate | +| [relative] | boolean | false | relative | + diff --git a/src/index.js b/src/index.js index 76118e0..e28be47 100644 --- a/src/index.js +++ b/src/index.js @@ -149,14 +149,20 @@ DateTime.prototype.minusBusiness = function({ days = ONE_DAY } = {}) { /** * Returns the difference in business days. Set relative to true if you need to support past dates. * @param {DateTime} targetDate - * @param {boolean} relative + * @typedef BusinessDiffConfig + * @property {boolean} [includeEndDate=false] includeEndDate + * @property {boolean} [relative=false] relative + * @param {BusinessDiffConfig} config * @returns {number} */ -DateTime.prototype.businessDiff = function(targetDate, relative = false) { +DateTime.prototype.businessDiff = function( + targetDate, + { includeEndDate = false, relative = false } = {} +) { let dt = this; let start = dt < targetDate ? dt : targetDate; let end = dt < targetDate ? targetDate : dt; - let daysDiff = 0; + let daysDiff = includeEndDate ? 1 : 0; let isSameDay = dt.hasSame(targetDate, 'day') && dt.hasSame(targetDate, 'month') && diff --git a/src/index.test.js b/src/index.test.js index 4d7b9a9..ad00d57 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -334,7 +334,7 @@ describe('business day diff', () => { expect(DateTime.local().businessDiff(targetDate)).toEqual(0); }); - it('knows there are three business days between two dates that are 3 business days apart', () => { + it('knows there are 3 business days between two dates that are 3 business days apart', () => { const myCompanyTakesNoHolidays = []; const startDate = DateTime.local().startOf('day'); const futureDate = startDate.plusBusiness({ days: 3 }); @@ -358,7 +358,25 @@ describe('business day diff', () => { holidayMatchers: myCompanyTakesNoHolidays, }); - expect(startDate.businessDiff(futureDate, true)).toEqual(3); - expect(startDate.businessDiff(pastDate, true)).toEqual(-3); + const config = { relative: true }; + + expect(startDate.businessDiff(futureDate, config)).toEqual(3); + expect(startDate.businessDiff(pastDate, config)).toEqual(-3); + }); + + it('knows there are 4 business days between two dates that are 3 business days apart but include the end date', () => { + const myCompanyTakesNoHolidays = []; + const startDate = DateTime.local().startOf('day'); + const futureDate = startDate.plusBusiness({ days: 3 }); + const pastDate = startDate.minusBusiness({ days: 3 }); + + startDate.setupBusiness({ + holidayMatchers: myCompanyTakesNoHolidays, + }); + + const config = { includeEndDate: true }; + + expect(startDate.businessDiff(futureDate, config)).toEqual(4); + expect(startDate.businessDiff(pastDate, config)).toEqual(4); }); }); From f04f736757555603e30c842eda7a9e4827b310d5 Mon Sep 17 00:00:00 2001 From: Kris Tremblay Date: Wed, 3 Mar 2021 12:14:17 -0600 Subject: [PATCH 06/16] Fix break in documentation. --- README.md | 28 ++++++++++++++++------------ src/index.js | 9 ++++++--- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 7f93520..7561de4 100644 --- a/README.md +++ b/README.md @@ -252,9 +252,8 @@ dt.isHoliday('middle-america', {some: 'stuff'}); ## Typedefs
-
BusinessDiffConfignumber
-

Returns the difference in business days. Set relative to true if you need to support past dates.

-
+
BusinessDiffConfig
+
@@ -272,6 +271,7 @@ dt.isHoliday('middle-america', {some: 'stuff'}); * [.isBusinessDay()](#DateTime+isBusinessDay) ⇒ boolean * [.plusBusiness([days])](#DateTime+plusBusiness) ⇒ [DateTime](#DateTime) * [.minusBusiness([days])](#DateTime+minusBusiness) ⇒ [DateTime](#DateTime) + * [.businessDiff(targetDate, config)](#DateTime+businessDiff) ⇒ number @@ -370,6 +370,18 @@ Subtracts business days to an existing DateTime instance. | --- | --- | --- | --- | | [days] | number | 1 | The number of business days to subtract. | + + +### dateTime.businessDiff(targetDate, config) ⇒ number +Returns the difference in business days. Set relative to true if you need to support past dates. + +**Kind**: instance method of [DateTime](#DateTime) + +| Param | Type | +| --- | --- | +| targetDate | [DateTime](#DateTime) | +| config | [BusinessDiffConfig](#BusinessDiffConfig) | + ## getEasterMonthAndDay(year) ⇒ Array.<number> @@ -384,16 +396,8 @@ Returns the month and day of Easter for a given year. -## BusinessDiffConfig ⇒ number -Returns the difference in business days. Set relative to true if you need to support past dates. - +## BusinessDiffConfig **Kind**: global typedef - -| Param | Type | -| --- | --- | -| targetDate | [DateTime](#DateTime) | -| config | [BusinessDiffConfig](#BusinessDiffConfig) | - **Properties** | Name | Type | Default | Description | diff --git a/src/index.js b/src/index.js index e28be47..0419a5b 100644 --- a/src/index.js +++ b/src/index.js @@ -146,12 +146,15 @@ DateTime.prototype.minusBusiness = function({ days = ONE_DAY } = {}) { return this.plusBusiness({ days: -days }); }; +/** + * @typedef BusinessDiffConfig + * @property {boolean} [includeEndDate=false] includeEndDate + * @property {boolean} [relative=false] relative + */ + /** * Returns the difference in business days. Set relative to true if you need to support past dates. * @param {DateTime} targetDate - * @typedef BusinessDiffConfig - * @property {boolean} [includeEndDate=false] includeEndDate - * @property {boolean} [relative=false] relative * @param {BusinessDiffConfig} config * @returns {number} */ From 092114f6a744c21ab1a78911725c843452302784 Mon Sep 17 00:00:00 2001 From: Kris Tremblay Date: Wed, 3 Mar 2021 12:17:34 -0600 Subject: [PATCH 07/16] Update businessDiff description. --- README.md | 2 +- src/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7561de4..633c886 100644 --- a/README.md +++ b/README.md @@ -373,7 +373,7 @@ Subtracts business days to an existing DateTime instance. ### dateTime.businessDiff(targetDate, config) ⇒ number -Returns the difference in business days. Set relative to true if you need to support past dates. +Returns the difference in business days. **Kind**: instance method of [DateTime](#DateTime) diff --git a/src/index.js b/src/index.js index 0419a5b..fe45e4b 100644 --- a/src/index.js +++ b/src/index.js @@ -153,7 +153,7 @@ DateTime.prototype.minusBusiness = function({ days = ONE_DAY } = {}) { */ /** - * Returns the difference in business days. Set relative to true if you need to support past dates. + * Returns the difference in business days. * @param {DateTime} targetDate * @param {BusinessDiffConfig} config * @returns {number} From bddb103c59ee7ac3d3dd39058886390605feb6c8 Mon Sep 17 00:00:00 2001 From: Kris Tremblay Date: Wed, 3 Mar 2021 12:20:19 -0600 Subject: [PATCH 08/16] Improved BusinessDiffConfig description --- README.md | 4 ++-- src/index.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 633c886..0261445 100644 --- a/README.md +++ b/README.md @@ -402,6 +402,6 @@ Returns the month and day of Easter for a given year. | Name | Type | Default | Description | | --- | --- | --- | --- | -| [includeEndDate] | boolean | false | includeEndDate | -| [relative] | boolean | false | relative | +| [includeEndDate] | boolean | false | include the end date in the calculation | +| [relative] | boolean | false | signs the return value as negative if end date is in the past | diff --git a/src/index.js b/src/index.js index fe45e4b..41e3256 100644 --- a/src/index.js +++ b/src/index.js @@ -148,8 +148,8 @@ DateTime.prototype.minusBusiness = function({ days = ONE_DAY } = {}) { /** * @typedef BusinessDiffConfig - * @property {boolean} [includeEndDate=false] includeEndDate - * @property {boolean} [relative=false] relative + * @property {boolean} [includeEndDate=false] - include the end date in the calculation + * @property {boolean} [relative=false] - signs the return value as negative if end date is in the past */ /** From 37e7a5a51513f71c50eecdace566d13c32e0782b Mon Sep 17 00:00:00 2001 From: Kris Tremblay Date: Thu, 29 Apr 2021 09:08:19 -0500 Subject: [PATCH 09/16] Update src/index.test.js Co-authored-by: Andrew Maidah --- src/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.test.js b/src/index.test.js index ad00d57..c7781e5 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -325,7 +325,7 @@ describe('time zone is carried over after a business-day operation', () => { }); }); -describe('business day diff', () => { +describe('diffBusiness()', () => { it('knows two identical DateTimes have a business day diff of 0', () => { const targetDate = DateTime.local() .startOf('day') From 0b8bf9c9b0af07534d2fc4906f622b32eca2f946 Mon Sep 17 00:00:00 2001 From: Kris Tremblay Date: Thu, 29 Apr 2021 09:09:32 -0500 Subject: [PATCH 10/16] Update src/index.js Co-authored-by: Andrew Maidah --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 41e3256..7ea9c06 100644 --- a/src/index.js +++ b/src/index.js @@ -158,7 +158,7 @@ DateTime.prototype.minusBusiness = function({ days = ONE_DAY } = {}) { * @param {BusinessDiffConfig} config * @returns {number} */ -DateTime.prototype.businessDiff = function( +DateTime.prototype.diffBusiness = function( targetDate, { includeEndDate = false, relative = false } = {} ) { From bfd9b845cafbeb898ee9ab77e68ef2f56718aa5d Mon Sep 17 00:00:00 2001 From: Kris Tremblay Date: Thu, 29 Apr 2021 10:31:07 -0500 Subject: [PATCH 11/16] Rename businessDiff diffBusiness for consistency --- README.md | 146 ++++++++++++++++++++++++---------------------- src/index.js | 4 +- src/index.test.js | 14 ++--- 3 files changed, 86 insertions(+), 78 deletions(-) diff --git a/README.md b/README.md index 0261445..1454b91 100644 --- a/README.md +++ b/README.md @@ -58,19 +58,19 @@ https://cdn.jsdelivr.net/npm/luxon-business-days@2.7.0/dist/index.js ``` ```html - + - + - + - + ``` ## Config @@ -166,7 +166,7 @@ const myHolidays = [ ]; dt.setupBusiness({ holidayMatchers: myHolidays }); -// Congrats, now your business will consider New Years, +// Congrats, now your business will consider New Years, // Christmas, and the two Kobe days as holidays. ``` @@ -185,7 +185,7 @@ let dt = DateTime.local(2019, 7, 3); dt = dt.plusBusiness(); // 7/5/19 - Friday dt = dt.plusBusiness({ days: 2 }); // 7/9/19 - Tuesday (Skipped through Saturday/Sunday) dt = dt.minusBusiness({ days: 2 }); // back to 7/5/19 -dt = dt.minusBusiness({ days: -2 }) // back to 7/9/19 +dt = dt.minusBusiness({ days: -2 }); // back to 7/9/19 dt = dt.plusBusiness({ days: -2 }); // back to 7/5/19 // Now do what you normally would with a DateTime instance. @@ -206,7 +206,7 @@ import { DateTime } from 'luxon-business-days'; */ const someCustomRegionalMatcher = (inst, region, someStuff) => { // does some matching based on region and data in someStuff -} +}; /** * @param {DateTime} - An instance of DateTime. @@ -214,23 +214,20 @@ const someCustomRegionalMatcher = (inst, region, someStuff) => { */ const anotherCustomMatcher = inst => { // does some matching, but doesn't need additional info -} +}; let dt = DateTime.local(); -const myHolidays = [ - someCustomRegionalMatcher, - anotherCustomMatcher, -]; +const myHolidays = [someCustomRegionalMatcher, anotherCustomMatcher]; dt.setupBusiness({ holidayMatchers: myHolidays }); // Pass any additional argument to all your matchers -dt.isHoliday('middle-america', {some: 'stuff'}); +dt.isHoliday('middle-america', { some: 'stuff' }); ``` ## Examples -* [Display a range of delivery dates for a shipment](https://codesandbox.io/s/luxon-business-days-range-example-tmb1d). +- [Display a range of delivery dates for a shipment](https://codesandbox.io/s/luxon-business-days-range-example-tmb1d). ## API @@ -252,77 +249,82 @@ dt.isHoliday('middle-america', {some: 'stuff'}); ## Typedefs
-
BusinessDiffConfig
+
diffBusinessConfig
## DateTime ⇐ [DateTime](#DateTime) -**Kind**: global variable -**Extends**: [DateTime](#DateTime) -* [DateTime](#DateTime) ⇐ [DateTime](#DateTime) - * [.availableHolidayMatchers](#DateTime+availableHolidayMatchers) : Object - * [.availableHolidayHelpers](#DateTime+availableHolidayHelpers) : Object - * [.setupBusiness([businessDays], [holidayMatchers])](#DateTime+setupBusiness) ⇐ [DateTime](#DateTime) - * [.clearBusinessSetup()](#DateTime+clearBusinessSetup) ⇐ [DateTime](#DateTime) - * [.isHoliday([...args])](#DateTime+isHoliday) ⇒ boolean - * [.isBusinessDay()](#DateTime+isBusinessDay) ⇒ boolean - * [.plusBusiness([days])](#DateTime+plusBusiness) ⇒ [DateTime](#DateTime) - * [.minusBusiness([days])](#DateTime+minusBusiness) ⇒ [DateTime](#DateTime) - * [.businessDiff(targetDate, config)](#DateTime+businessDiff) ⇒ number +**Kind**: global variable +**Extends**: [DateTime](#DateTime) + +- [DateTime](#DateTime) ⇐ [DateTime](#DateTime) + - [.availableHolidayMatchers](#DateTime+availableHolidayMatchers) : Object + - [.availableHolidayHelpers](#DateTime+availableHolidayHelpers) : Object + - [.setupBusiness([businessDays], [holidayMatchers])](#DateTime+setupBusiness) ⇐ [DateTime](#DateTime) + - [.clearBusinessSetup()](#DateTime+clearBusinessSetup) ⇐ [DateTime](#DateTime) + - [.isHoliday([...args])](#DateTime+isHoliday) ⇒ boolean + - [.isBusinessDay()](#DateTime+isBusinessDay) ⇒ boolean + - [.plusBusiness([days])](#DateTime+plusBusiness) ⇒ [DateTime](#DateTime) + - [.minusBusiness([days])](#DateTime+minusBusiness) ⇒ [DateTime](#DateTime) + - [.diffBusiness(targetDate, config)](#DateTime+diffBusiness) ⇒ number ### dateTime.availableHolidayMatchers : Object + All built-in holiday matchers. **Kind**: instance property of [DateTime](#DateTime) **Extends**: [DateTime](#DateTime) **Properties** -| Name | Type | Description | -| --- | --- | --- | -| isNewYearsDay | function | A provided holiday matcher. | -| isMLKDay | function | A provided holiday matcher. | -| isEasterDay | function | A provided holiday matcher. | -| isMemorialDay | function | A provided holiday matcher. | +| Name | Type | Description | +| ----------------- | --------------------- | --------------------------- | +| isNewYearsDay | function | A provided holiday matcher. | +| isMLKDay | function | A provided holiday matcher. | +| isEasterDay | function | A provided holiday matcher. | +| isMemorialDay | function | A provided holiday matcher. | | isIndependanceDay | function | A provided holiday matcher. | -| isLaborDay | function | A provided holiday matcher. | -| isColumbusDay | function | A provided holiday matcher. | +| isLaborDay | function | A provided holiday matcher. | +| isColumbusDay | function | A provided holiday matcher. | | isThanksgivingDay | function | A provided holiday matcher. | -| isChristmasDay | function | A provided holiday matcher. | +| isChristmasDay | function | A provided holiday matcher. | ### dateTime.availableHolidayHelpers : Object + Exposes all available holiday helpers to a DateTime instance. **Kind**: instance property of [DateTime](#DateTime) **Extends**: [DateTime](#DateTime) **Properties** -| Name | Type | Description | -| --- | --- | --- | +| Name | Type | Description | +| -------------------- | --------------------- | ----------------------------------------------------------------------------------- | | getEasterMonthAndDay | function | A provided holiday helper function that can be helpful for custom holiday matchers. | ### dateTime.setupBusiness([businessDays], [holidayMatchers]) ⇐ [DateTime](#DateTime) + Sets up business days and holiday matchers globally for all DateTime instances. **Kind**: instance method of [DateTime](#DateTime) -**Extends**: [DateTime](#DateTime) +**Extends**: [DateTime](#DateTime) -| Param | Type | Default | Description | -| --- | --- | --- | --- | -| [businessDays] | Array.<number> | DEFAULT_BUSINESS_DAYS | The working business days for the business. | +| Param | Type | Default | Description | +| ----------------- | ------------------------------------- | ------------------------------------- | ------------------------------------------------------------------------------------- | +| [businessDays] | Array.<number> | DEFAULT_BUSINESS_DAYS | The working business days for the business. | | [holidayMatchers] | Array.<function()> | DEFAULT_HOLIDAY_MATCHERS | The holiday matchers used to check if a particular day is a holiday for the business. | ### dateTime.clearBusinessSetup() ⇐ [DateTime](#DateTime) + Clears business setup globally from all DateTime instances. **Kind**: instance method of [DateTime](#DateTime) @@ -330,18 +332,20 @@ Clears business setup globally from all DateTime instances. ### dateTime.isHoliday([...args]) ⇒ boolean + Checks if DateTime instance is a holiday by checking against all holiday matchers. **Kind**: instance method of [DateTime](#DateTime) -**Extends**: [DateTime](#DateTime) +**Extends**: [DateTime](#DateTime) -| Param | Type | Description | -| --- | --- | --- | +| Param | Type | Description | +| --------- | --------------- | ----------------------------------------------------------------- | | [...args] | \* | Any additional arguments to pass through to each holiday matcher. | ### dateTime.isBusinessDay() ⇒ boolean + Checks if DateTime instance is a business day. **Kind**: instance method of [DateTime](#DateTime) @@ -349,59 +353,63 @@ Checks if DateTime instance is a business day. ### dateTime.plusBusiness([days]) ⇒ [DateTime](#DateTime) + Adds business days to an existing DateTime instance. **Kind**: instance method of [DateTime](#DateTime) -**Extends**: [DateTime](#DateTime) +**Extends**: [DateTime](#DateTime) -| Param | Type | Default | Description | -| --- | --- | --- | --- | +| Param | Type | Default | Description | +| ------ | ------------------- | -------------- | ----------------------------------- | | [days] | number | 1 | The number of business days to add. | ### dateTime.minusBusiness([days]) ⇒ [DateTime](#DateTime) + Subtracts business days to an existing DateTime instance. **Kind**: instance method of [DateTime](#DateTime) -**Extends**: [DateTime](#DateTime) +**Extends**: [DateTime](#DateTime) -| Param | Type | Default | Description | -| --- | --- | --- | --- | +| Param | Type | Default | Description | +| ------ | ------------------- | -------------- | ---------------------------------------- | | [days] | number | 1 | The number of business days to subtract. | - + + +### dateTime.diffBusiness(targetDate, config) ⇒ number -### dateTime.businessDiff(targetDate, config) ⇒ number Returns the difference in business days. -**Kind**: instance method of [DateTime](#DateTime) +**Kind**: instance method of [DateTime](#DateTime) -| Param | Type | -| --- | --- | -| targetDate | [DateTime](#DateTime) | -| config | [BusinessDiffConfig](#BusinessDiffConfig) | +| Param | Type | +| ---------- | ------------------------------------------------------ | +| targetDate | [DateTime](#DateTime) | +| config | [diffBusinessConfig](#diffBusinessConfig) | ## getEasterMonthAndDay(year) ⇒ Array.<number> + Returns the month and day of Easter for a given year. **Kind**: global function -**Returns**: Array.<number> - Returns month and day via `[month, day]`. +**Returns**: Array.<number> - Returns month and day via `[month, day]`. + +| Param | Type | +| ----- | ------------------- | +| year | number | -| Param | Type | -| --- | --- | -| year | number | + - +## diffBusinessConfig -## BusinessDiffConfig **Kind**: global typedef **Properties** -| Name | Type | Default | Description | -| --- | --- | --- | --- | -| [includeEndDate] | boolean | false | include the end date in the calculation | -| [relative] | boolean | false | signs the return value as negative if end date is in the past | - +| Name | Type | Default | Description | +| ---------------- | -------------------- | ------------------ | ------------------------------------------------------------- | +| [includeEndDate] | boolean | false | include the end date in the calculation | +| [relative] | boolean | false | signs the return value as negative if end date is in the past | diff --git a/src/index.js b/src/index.js index 7ea9c06..313291e 100644 --- a/src/index.js +++ b/src/index.js @@ -147,7 +147,7 @@ DateTime.prototype.minusBusiness = function({ days = ONE_DAY } = {}) { }; /** - * @typedef BusinessDiffConfig + * @typedef DiffBusinessConfig * @property {boolean} [includeEndDate=false] - include the end date in the calculation * @property {boolean} [relative=false] - signs the return value as negative if end date is in the past */ @@ -155,7 +155,7 @@ DateTime.prototype.minusBusiness = function({ days = ONE_DAY } = {}) { /** * Returns the difference in business days. * @param {DateTime} targetDate - * @param {BusinessDiffConfig} config + * @param {DiffBusinessConfig} config * @returns {number} */ DateTime.prototype.diffBusiness = function( diff --git a/src/index.test.js b/src/index.test.js index c7781e5..f92b66b 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -331,7 +331,7 @@ describe('diffBusiness()', () => { .startOf('day') .plus({ hours: 2 }); - expect(DateTime.local().businessDiff(targetDate)).toEqual(0); + expect(DateTime.local().diffBusiness(targetDate)).toEqual(0); }); it('knows there are 3 business days between two dates that are 3 business days apart', () => { @@ -344,8 +344,8 @@ describe('diffBusiness()', () => { holidayMatchers: myCompanyTakesNoHolidays, }); - expect(startDate.businessDiff(futureDate)).toEqual(3); - expect(startDate.businessDiff(pastDate)).toEqual(3); + expect(startDate.diffBusiness(futureDate)).toEqual(3); + expect(startDate.diffBusiness(pastDate)).toEqual(3); }); it('knows diff is negative for the past and positive for the future if relative is specified', () => { @@ -360,8 +360,8 @@ describe('diffBusiness()', () => { const config = { relative: true }; - expect(startDate.businessDiff(futureDate, config)).toEqual(3); - expect(startDate.businessDiff(pastDate, config)).toEqual(-3); + expect(startDate.diffBusiness(futureDate, config)).toEqual(3); + expect(startDate.diffBusiness(pastDate, config)).toEqual(-3); }); it('knows there are 4 business days between two dates that are 3 business days apart but include the end date', () => { @@ -376,7 +376,7 @@ describe('diffBusiness()', () => { const config = { includeEndDate: true }; - expect(startDate.businessDiff(futureDate, config)).toEqual(4); - expect(startDate.businessDiff(pastDate, config)).toEqual(4); + expect(startDate.diffBusiness(futureDate, config)).toEqual(4); + expect(startDate.diffBusiness(pastDate, config)).toEqual(4); }); }); From e4f92b0eb9cabbd8556a1ae0b474269445a4c16f Mon Sep 17 00:00:00 2001 From: Kris Tremblay Date: Thu, 29 Apr 2021 10:33:39 -0500 Subject: [PATCH 12/16] Readme regen --- README.md | 142 ++++++++++++++++++++++++++---------------------------- 1 file changed, 67 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index 1454b91..c19d56b 100644 --- a/README.md +++ b/README.md @@ -58,19 +58,19 @@ https://cdn.jsdelivr.net/npm/luxon-business-days@2.7.0/dist/index.js ``` ```html - + - + - + - + ``` ## Config @@ -166,7 +166,7 @@ const myHolidays = [ ]; dt.setupBusiness({ holidayMatchers: myHolidays }); -// Congrats, now your business will consider New Years, +// Congrats, now your business will consider New Years, // Christmas, and the two Kobe days as holidays. ``` @@ -185,7 +185,7 @@ let dt = DateTime.local(2019, 7, 3); dt = dt.plusBusiness(); // 7/5/19 - Friday dt = dt.plusBusiness({ days: 2 }); // 7/9/19 - Tuesday (Skipped through Saturday/Sunday) dt = dt.minusBusiness({ days: 2 }); // back to 7/5/19 -dt = dt.minusBusiness({ days: -2 }); // back to 7/9/19 +dt = dt.minusBusiness({ days: -2 }) // back to 7/9/19 dt = dt.plusBusiness({ days: -2 }); // back to 7/5/19 // Now do what you normally would with a DateTime instance. @@ -206,7 +206,7 @@ import { DateTime } from 'luxon-business-days'; */ const someCustomRegionalMatcher = (inst, region, someStuff) => { // does some matching based on region and data in someStuff -}; +} /** * @param {DateTime} - An instance of DateTime. @@ -214,20 +214,23 @@ const someCustomRegionalMatcher = (inst, region, someStuff) => { */ const anotherCustomMatcher = inst => { // does some matching, but doesn't need additional info -}; +} let dt = DateTime.local(); -const myHolidays = [someCustomRegionalMatcher, anotherCustomMatcher]; +const myHolidays = [ + someCustomRegionalMatcher, + anotherCustomMatcher, +]; dt.setupBusiness({ holidayMatchers: myHolidays }); // Pass any additional argument to all your matchers -dt.isHoliday('middle-america', { some: 'stuff' }); +dt.isHoliday('middle-america', {some: 'stuff'}); ``` ## Examples -- [Display a range of delivery dates for a shipment](https://codesandbox.io/s/luxon-business-days-range-example-tmb1d). +* [Display a range of delivery dates for a shipment](https://codesandbox.io/s/luxon-business-days-range-example-tmb1d). ## API @@ -249,82 +252,77 @@ dt.isHoliday('middle-america', { some: 'stuff' }); ## Typedefs
-
diffBusinessConfig
+
DiffBusinessConfig
## DateTime ⇐ [DateTime](#DateTime) - **Kind**: global variable -**Extends**: [DateTime](#DateTime) - -- [DateTime](#DateTime) ⇐ [DateTime](#DateTime) - - [.availableHolidayMatchers](#DateTime+availableHolidayMatchers) : Object - - [.availableHolidayHelpers](#DateTime+availableHolidayHelpers) : Object - - [.setupBusiness([businessDays], [holidayMatchers])](#DateTime+setupBusiness) ⇐ [DateTime](#DateTime) - - [.clearBusinessSetup()](#DateTime+clearBusinessSetup) ⇐ [DateTime](#DateTime) - - [.isHoliday([...args])](#DateTime+isHoliday) ⇒ boolean - - [.isBusinessDay()](#DateTime+isBusinessDay) ⇒ boolean - - [.plusBusiness([days])](#DateTime+plusBusiness) ⇒ [DateTime](#DateTime) - - [.minusBusiness([days])](#DateTime+minusBusiness) ⇒ [DateTime](#DateTime) - - [.diffBusiness(targetDate, config)](#DateTime+diffBusiness) ⇒ number +**Extends**: [DateTime](#DateTime) + +* [DateTime](#DateTime) ⇐ [DateTime](#DateTime) + * [.availableHolidayMatchers](#DateTime+availableHolidayMatchers) : Object + * [.availableHolidayHelpers](#DateTime+availableHolidayHelpers) : Object + * [.setupBusiness([businessDays], [holidayMatchers])](#DateTime+setupBusiness) ⇐ [DateTime](#DateTime) + * [.clearBusinessSetup()](#DateTime+clearBusinessSetup) ⇐ [DateTime](#DateTime) + * [.isHoliday([...args])](#DateTime+isHoliday) ⇒ boolean + * [.isBusinessDay()](#DateTime+isBusinessDay) ⇒ boolean + * [.plusBusiness([days])](#DateTime+plusBusiness) ⇒ [DateTime](#DateTime) + * [.minusBusiness([days])](#DateTime+minusBusiness) ⇒ [DateTime](#DateTime) + * [.diffBusiness(targetDate, config)](#DateTime+diffBusiness) ⇒ number ### dateTime.availableHolidayMatchers : Object - All built-in holiday matchers. **Kind**: instance property of [DateTime](#DateTime) **Extends**: [DateTime](#DateTime) **Properties** -| Name | Type | Description | -| ----------------- | --------------------- | --------------------------- | -| isNewYearsDay | function | A provided holiday matcher. | -| isMLKDay | function | A provided holiday matcher. | -| isEasterDay | function | A provided holiday matcher. | -| isMemorialDay | function | A provided holiday matcher. | +| Name | Type | Description | +| --- | --- | --- | +| isNewYearsDay | function | A provided holiday matcher. | +| isMLKDay | function | A provided holiday matcher. | +| isEasterDay | function | A provided holiday matcher. | +| isMemorialDay | function | A provided holiday matcher. | | isIndependanceDay | function | A provided holiday matcher. | -| isLaborDay | function | A provided holiday matcher. | -| isColumbusDay | function | A provided holiday matcher. | +| isLaborDay | function | A provided holiday matcher. | +| isColumbusDay | function | A provided holiday matcher. | | isThanksgivingDay | function | A provided holiday matcher. | -| isChristmasDay | function | A provided holiday matcher. | +| isChristmasDay | function | A provided holiday matcher. | ### dateTime.availableHolidayHelpers : Object - Exposes all available holiday helpers to a DateTime instance. **Kind**: instance property of [DateTime](#DateTime) **Extends**: [DateTime](#DateTime) **Properties** -| Name | Type | Description | -| -------------------- | --------------------- | ----------------------------------------------------------------------------------- | +| Name | Type | Description | +| --- | --- | --- | | getEasterMonthAndDay | function | A provided holiday helper function that can be helpful for custom holiday matchers. | ### dateTime.setupBusiness([businessDays], [holidayMatchers]) ⇐ [DateTime](#DateTime) - Sets up business days and holiday matchers globally for all DateTime instances. **Kind**: instance method of [DateTime](#DateTime) -**Extends**: [DateTime](#DateTime) +**Extends**: [DateTime](#DateTime) -| Param | Type | Default | Description | -| ----------------- | ------------------------------------- | ------------------------------------- | ------------------------------------------------------------------------------------- | -| [businessDays] | Array.<number> | DEFAULT_BUSINESS_DAYS | The working business days for the business. | +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| [businessDays] | Array.<number> | DEFAULT_BUSINESS_DAYS | The working business days for the business. | | [holidayMatchers] | Array.<function()> | DEFAULT_HOLIDAY_MATCHERS | The holiday matchers used to check if a particular day is a holiday for the business. | ### dateTime.clearBusinessSetup() ⇐ [DateTime](#DateTime) - Clears business setup globally from all DateTime instances. **Kind**: instance method of [DateTime](#DateTime) @@ -332,20 +330,18 @@ Clears business setup globally from all DateTime instances. ### dateTime.isHoliday([...args]) ⇒ boolean - Checks if DateTime instance is a holiday by checking against all holiday matchers. **Kind**: instance method of [DateTime](#DateTime) -**Extends**: [DateTime](#DateTime) +**Extends**: [DateTime](#DateTime) -| Param | Type | Description | -| --------- | --------------- | ----------------------------------------------------------------- | +| Param | Type | Description | +| --- | --- | --- | | [...args] | \* | Any additional arguments to pass through to each holiday matcher. | ### dateTime.isBusinessDay() ⇒ boolean - Checks if DateTime instance is a business day. **Kind**: instance method of [DateTime](#DateTime) @@ -353,63 +349,59 @@ Checks if DateTime instance is a business day. ### dateTime.plusBusiness([days]) ⇒ [DateTime](#DateTime) - Adds business days to an existing DateTime instance. **Kind**: instance method of [DateTime](#DateTime) -**Extends**: [DateTime](#DateTime) +**Extends**: [DateTime](#DateTime) -| Param | Type | Default | Description | -| ------ | ------------------- | -------------- | ----------------------------------- | +| Param | Type | Default | Description | +| --- | --- | --- | --- | | [days] | number | 1 | The number of business days to add. | ### dateTime.minusBusiness([days]) ⇒ [DateTime](#DateTime) - Subtracts business days to an existing DateTime instance. **Kind**: instance method of [DateTime](#DateTime) -**Extends**: [DateTime](#DateTime) +**Extends**: [DateTime](#DateTime) -| Param | Type | Default | Description | -| ------ | ------------------- | -------------- | ---------------------------------------- | +| Param | Type | Default | Description | +| --- | --- | --- | --- | | [days] | number | 1 | The number of business days to subtract. | ### dateTime.diffBusiness(targetDate, config) ⇒ number - Returns the difference in business days. -**Kind**: instance method of [DateTime](#DateTime) +**Kind**: instance method of [DateTime](#DateTime) -| Param | Type | -| ---------- | ------------------------------------------------------ | -| targetDate | [DateTime](#DateTime) | -| config | [diffBusinessConfig](#diffBusinessConfig) | +| Param | Type | +| --- | --- | +| targetDate | [DateTime](#DateTime) | +| config | [DiffBusinessConfig](#DiffBusinessConfig) | ## getEasterMonthAndDay(year) ⇒ Array.<number> - Returns the month and day of Easter for a given year. **Kind**: global function -**Returns**: Array.<number> - Returns month and day via `[month, day]`. - -| Param | Type | -| ----- | ------------------- | -| year | number | +**Returns**: Array.<number> - Returns month and day via `[month, day]`. - +| Param | Type | +| --- | --- | +| year | number | -## diffBusinessConfig + +## DiffBusinessConfig **Kind**: global typedef **Properties** -| Name | Type | Default | Description | -| ---------------- | -------------------- | ------------------ | ------------------------------------------------------------- | -| [includeEndDate] | boolean | false | include the end date in the calculation | -| [relative] | boolean | false | signs the return value as negative if end date is in the past | +| Name | Type | Default | Description | +| --- | --- | --- | --- | +| [includeEndDate] | boolean | false | include the end date in the calculation | +| [relative] | boolean | false | signs the return value as negative if end date is in the past | + From a26325eb87b42c4b186defacd9c8e84b600cf23a Mon Sep 17 00:00:00 2001 From: Kris Tremblay Date: Thu, 29 Apr 2021 11:04:56 -0500 Subject: [PATCH 13/16] Removed start of day from tests --- src/index.test.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/index.test.js b/src/index.test.js index f92b66b..5725870 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -327,16 +327,14 @@ describe('time zone is carried over after a business-day operation', () => { describe('diffBusiness()', () => { it('knows two identical DateTimes have a business day diff of 0', () => { - const targetDate = DateTime.local() - .startOf('day') - .plus({ hours: 2 }); + const targetDate = DateTime.local().plus({ hours: 2 }); expect(DateTime.local().diffBusiness(targetDate)).toEqual(0); }); it('knows there are 3 business days between two dates that are 3 business days apart', () => { const myCompanyTakesNoHolidays = []; - const startDate = DateTime.local().startOf('day'); + const startDate = DateTime.local(); const futureDate = startDate.plusBusiness({ days: 3 }); const pastDate = startDate.minusBusiness({ days: 3 }); @@ -350,7 +348,7 @@ describe('diffBusiness()', () => { it('knows diff is negative for the past and positive for the future if relative is specified', () => { const myCompanyTakesNoHolidays = []; - const startDate = DateTime.local().startOf('day'); + const startDate = DateTime.local(); const futureDate = startDate.plusBusiness({ days: 3 }); const pastDate = startDate.minusBusiness({ days: 3 }); @@ -366,7 +364,7 @@ describe('diffBusiness()', () => { it('knows there are 4 business days between two dates that are 3 business days apart but include the end date', () => { const myCompanyTakesNoHolidays = []; - const startDate = DateTime.local().startOf('day'); + const startDate = DateTime.local(); const futureDate = startDate.plusBusiness({ days: 3 }); const pastDate = startDate.minusBusiness({ days: 3 }); From c2288c16f7e0ef7c2c6afffc4735eebfa667777f Mon Sep 17 00:00:00 2001 From: Kris Tremblay Date: Sat, 1 May 2021 15:06:54 -0500 Subject: [PATCH 14/16] Update src/index.test.js Put startOf call back in. Co-authored-by: Andrew Maidah --- src/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.test.js b/src/index.test.js index 5725870..5433882 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -327,7 +327,7 @@ describe('time zone is carried over after a business-day operation', () => { describe('diffBusiness()', () => { it('knows two identical DateTimes have a business day diff of 0', () => { - const targetDate = DateTime.local().plus({ hours: 2 }); + const targetDate = DateTime.local().startOf('day').plus({ hours: 2 }); expect(DateTime.local().diffBusiness(targetDate)).toEqual(0); }); From 2f2a411526b801238d0d0df02c5a6bd188c51447 Mon Sep 17 00:00:00 2001 From: Kris Tremblay Date: Sat, 1 May 2021 16:49:22 -0500 Subject: [PATCH 15/16] Test cleanup and includeEndDate fix --- src/index.js | 4 +++- src/index.test.js | 11 +++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 313291e..5f040e1 100644 --- a/src/index.js +++ b/src/index.js @@ -165,7 +165,9 @@ DateTime.prototype.diffBusiness = function( let dt = this; let start = dt < targetDate ? dt : targetDate; let end = dt < targetDate ? targetDate : dt; - let daysDiff = includeEndDate ? 1 : 0; + let daysDiff = Number( + includeEndDate && end.isBusinessDay() && !end.isHoliday() + ); let isSameDay = dt.hasSame(targetDate, 'day') && dt.hasSame(targetDate, 'month') && diff --git a/src/index.test.js b/src/index.test.js index 5433882..6dfd5e8 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -326,15 +326,18 @@ describe('time zone is carried over after a business-day operation', () => { }); describe('diffBusiness()', () => { + // Pick a start date we know is a valid business day + const defaultStartDate = DateTime.fromISO('2021-05-03'); + it('knows two identical DateTimes have a business day diff of 0', () => { - const targetDate = DateTime.local().startOf('day').plus({ hours: 2 }); + const targetDate = defaultStartDate.startOf('day').plus({ hours: 2 }); expect(DateTime.local().diffBusiness(targetDate)).toEqual(0); }); it('knows there are 3 business days between two dates that are 3 business days apart', () => { const myCompanyTakesNoHolidays = []; - const startDate = DateTime.local(); + const startDate = defaultStartDate.startOf('day'); const futureDate = startDate.plusBusiness({ days: 3 }); const pastDate = startDate.minusBusiness({ days: 3 }); @@ -348,7 +351,7 @@ describe('diffBusiness()', () => { it('knows diff is negative for the past and positive for the future if relative is specified', () => { const myCompanyTakesNoHolidays = []; - const startDate = DateTime.local(); + const startDate = defaultStartDate.startOf('day'); const futureDate = startDate.plusBusiness({ days: 3 }); const pastDate = startDate.minusBusiness({ days: 3 }); @@ -364,7 +367,7 @@ describe('diffBusiness()', () => { it('knows there are 4 business days between two dates that are 3 business days apart but include the end date', () => { const myCompanyTakesNoHolidays = []; - const startDate = DateTime.local(); + const startDate = defaultStartDate.startOf('day'); const futureDate = startDate.plusBusiness({ days: 3 }); const pastDate = startDate.minusBusiness({ days: 3 }); From 944ebb5a05c6beb083ce6487723f4d2d4b28641e Mon Sep 17 00:00:00 2001 From: Kris Tremblay Date: Sat, 1 May 2021 16:53:45 -0500 Subject: [PATCH 16/16] Fixed missing coverage --- src/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.test.js b/src/index.test.js index 6dfd5e8..0d28855 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -332,7 +332,7 @@ describe('diffBusiness()', () => { it('knows two identical DateTimes have a business day diff of 0', () => { const targetDate = defaultStartDate.startOf('day').plus({ hours: 2 }); - expect(DateTime.local().diffBusiness(targetDate)).toEqual(0); + expect(defaultStartDate.diffBusiness(targetDate)).toEqual(0); }); it('knows there are 3 business days between two dates that are 3 business days apart', () => {