Calculate business days difference between two dates

Hi team
So, I’m trying to calculate the business days difference between dates dates.
I was able to find the day difference using the Date & Time comparisons at the IF node as shown in the screenshot below. But the business days difference is still a mystery to me.

Thanks!

Hi @bn99 - welcome to the community :tada:

Could you follow the template when you create a post, and also share your workflow? You can copy (ctrl/cmd + c) from the workflow editor and paste it here between three backticks (```) above and below it - and it will share your workflow. Seeing the whole thing and knowing what version of n8n you’re using may help here :slight_smile:

1 Like

Sure thing

n8n version 0.228.2

1 Like

I’ve used a code/function node for this in the past, not exactly the same problem, but might help. Adding 5 days to a due date

function addBusinessDays(start, businessDays, inclusive=false) {
    end = start;
    if (inclusive && start.weekday > 0 && start.weekday < 6 ) {
        businessDays--;
    }
    while (businessDays > 0) {
        end = end.plus({days: 1});
        // If is weekday
        if (end.weekday > 0 && end.weekday < 6) {
            // console.log(`${businessDays}: ${end.weekdayShort} is business`)
            businessDays--;
        } else {
            // console.log(`${businessDays}: ${end.weekdayShort} is not counted`)
        }
    }
    return end.set({ hour: 23, minute: 59, second: 59, millisecond: 999 })
}

item.endDate = addBusinessDays(DateTime.fromISO($json["Due Date"]), 5, true).toString();

return item;
2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.