views:

57

answers:

1

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.

+2  A: 

Take a look at business_time. It can be used for the second half of what you're asking.

e.g.

4.business_days.from_now
8.business_days.after(some_date)

Unfortunately, from the notes it appears the author is reluctant to add something like business_duration_between which would cover the first half of your question.

Update

Below is a method to count the business days between two dates. You can fine tune this to handle the cases that Tipx mentions in the way that you would like.

def business_days_between(date1, date2)
  business_days = 0
  date = date2
  while date > date1
   business_days = business_days + 1 unless date.wday == 0 or date.wday == 6
   date = date - 1.day
  end
  business_days
end
mikej
The combo of business_time plus your snippet is perfect.
Shpigford