How to calculate next and previous business days in Rails?
Here is a faster method that uses a simple calculation instead of iterating over the days.
class Time
def shift_weekdays(num_weekdays)
base = self
# corner case: self falls on a Sat or Sun then treat like its the next Monday
case self.wday
when 0
base = self + 1.day
when 6
base = self + 2.day
end
day_of_week = base.wday - 1 # Monday is 0
weekends = (day_of_week + num_weekdays) / 5
base + (weekends*2).days + num_weekdays.days
end
end
The method is on class Time but can be used on Date class as well.