eduolalo / moment-business-days

This is a momentJS plugin that allows you to use only business days (Monday to Friday)
MIT License
240 stars 67 forks source link

businessAdd() return wrong value #81

Closed rotimi-best closed 4 years ago

rotimi-best commented 4 years ago

Case 1

If today is Friday and I add 1 day it gives me Sunday. Shouldn't it be Monday if Sunday and Saturday are not business days.

moment.updateLocale('us', {
    workingWeekdays: [1, 2, 3, 4, 5]
});

const currentDate = new Date(); // 2nd November 2019, Saturday

currentDate.setDate(currentDate.getDate() - 1); // 1st November 2019, Friday
console.log(currentDate) // 2019-11-01T10:05:53.289Z
console.log(currentDate.getDay()) // 5

// For some reason Friday `+1` returns Sunday. I expected Monday
console.log(moment(currentDate, 'DD-MM-YY').startOf('day').businessAdd(1)._d) // 2019-11-03T22:00:00.000Z

Case 2

If today is Saturday and I add 2 days I expect it to skip sunday. Meaning I should get Tuesday but instead I get Monday.

const currentDate = new Date(); // 2nd November 2019, Saturday
console.log(currentDate) // 2019-11-02T10:12:34.979Z
console.log(currentDate.getDay()) // 6

// Here I get 4th of november instead of 5th considering that the 3rd (Sunday)
console.log(moment(currentDate, 'DD-MM-YY').startOf('day').businessAdd(2)._d) // 2019-11-04T22:00:00.000Z

Please let me know what you think about the following cases.

mcdado commented 4 years ago

This has to do with timezone. The internal date is represented as UTC, that's why it is the day before at 22:00 (10pm). I assume you're 2 hours after UTC where you are. If you are logging in the following way you get the "right" date:

console.log(moment(currentDate, 'DD-MM-YY').startOf('day').businessAdd(2).format('DD-MM-YY') // 04-11-19

Of course you can use _d.toTimeString() or _d.toDateString() to get the representation you want. Anyway 2019-11-03T22:00:00.000Z that you got is till correct. Is midnight of Monday where you are.