diff --git a/package.json b/package.json index 546893e..ef47010 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dayjs-business-time", - "version": "1.0.3", + "version": "1.0.4", "author": "Alexsander Lira", "main": "dist/index.js", "types": "types/index.d.ts", diff --git a/src/index.ts b/src/index.ts index b21d5a0..ac36bc7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -374,9 +374,7 @@ const businessTime = ( break; } else if (from.isSameOrAfter(start) && from.isSameOrBefore(end)) { diff += end.diff(from, 'minutes'); - } else { - diff += end.diff(start, 'minutes'); - } + } } return diff ? diff * multiplier : 0; diff --git a/test/businessHoursDiff.spec.ts b/test/businessHoursDiff.spec.ts index be2043c..73b6367 100644 --- a/test/businessHoursDiff.spec.ts +++ b/test/businessHoursDiff.spec.ts @@ -89,12 +89,32 @@ describe('Business Hours Diff', () => { }); it('should get the -3.5 business hours diff between 2 times in the same day with timezone', () => { - const start = dayjs('2021-05-17T15:30:00-03:00'); - const end = dayjs('2021-05-17T12:00:00-03:00'); + const start = dayjs('2021-05-17T13:30:00-03:00'); + const end = dayjs('2021-05-17T10:00:00-03:00'); const diff = start.businessHoursDiff(end); expect(diff).toBeDefined(); expect(diff).toBe(-3.5); }); + + it('should get the 1 business hour diff between 2 times in the same business hours segment in a day with 2 segments', () => { + const start = dayjs('2021-05-26 16:00:00'); + const end = dayjs('2021-05-26 17:00:00'); + + const diff = start.businessHoursDiff(end); + + expect(diff).toBeDefined(); + expect(diff).toBe(1); + }); + + it('should get the 9.5 business hour diff between 2 times in 2 business days with a day with 2 segments', () => { + const start = dayjs('2021-05-25 14:30:00'); + const end = dayjs('2021-05-26 17:00:00'); + + const diff = start.businessHoursDiff(end); + + expect(diff).toBeDefined(); + expect(diff).toBe(9.5); + }); });