Calculate number of business days between two days

Viewed 21699

I need to calculate the number of business days between two dates. How can I pull that off using Ruby (or Rails...if there are Rails-specific helpers).

Likewise, I'd like to be able to add business days to a given date.

So if a date fell on a Thursday and I added 3 business days, it would return the next Tuesday.

8 Answers

Here is my (non gem and non holiday) weekday count example:

first_date = Date.new(2016,1,5)
second_date = Date.new(2016,1,12)
count = 0
(first_date...second_date).each{|d| count+=1 if (1..5).include?(d.wday)}
count
Related