How to Ignore Weekends in Go
Given a date, we’re looking at how you can add a duration to it, like a month or two, but ignoring weekends. This is usually most used for businesses focused on working-days - but it could easily be adapted to do the opposite, only weekends. It’s built around the standard lib time package so no extra packages needed.
We’ve got some example code below that makes a date and adds on 14 days, while skipping the weekend days out. We are doing this manually just checking each day on the
Weekday() function and comparing it to
Most of the logic will be handled inside
addDaysIgnoreWeekends() as it steps through each day. Therefore this function could also easily be adapted to ignore bank-holidays and generic/religous holidays like christmas.