Calculate the number of working days between two dates
Function
Iterates over date range.
# wdays is an array with the days of the week # to exclude days (eg: wdays = [0,6] for sunday and saturday ) def calculate_working_days(d1,d2,wdays) diff = d2 - d1 holidays = 0 ret = (d2-d1).divmod(7) holidays = ret[0].truncate * wdays.length d1 = d2 - ret[1] while(d1 <= d2) if wdays.include?(d1.wday) holidays += 1 end d1 += 1 end diff - holidays end
Iterates over date range.
d1 = Date.new( 2006, 12, 1 )
d2 = Date.new( 2007, 1, 15 )
weekdays = (d1..d2).reject { |d| [0,6].include? d.wday }
weekdays.length