andruhon / moment-weekday-calc

MIT License
85 stars 13 forks source link

Weird wrong calculation for certain date, when start > end date #24

Open hdoan741 opened 2 years ago

hdoan741 commented 2 years ago
const workdays1 = moment().weekdayCalc('21 Feb 2016', '31 Mar 2016', [1,2,3,4,5]); 
// 29
const workdays2 = moment().weekdayCalc('31 Mar 2016', '21 Feb 2016', [1,2,3,4,5]); 
// 28

There is a bizarre problem where the weekdayCalc is different when the start date & end date is reversed. Specifically, if start > end, some time the result is 1 day short of the correct value.

In the source code, I could see that if start > end, their position are switched. The calculation should have been the same for both. I don't know how this could happen. See the example above.

andruhon commented 2 years ago

@manhhung741 Could you please advice which version do you use? I'm pretty sure it should throw an error if start date is after end date.

hdoan-codaio commented 2 years ago

The version I'm using is "moment-weekday-calc": "^1.1.4",

I thought this code would flip start & end date if start > end date

https://github.com/andruhon/moment-weekday-calc/blob/master/src/moment-weekday-calc.js#L202-L207