Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
1 total  XML / RSS feed 

Calculate the number of working days between two dates

Function
   # 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
« Newer Snippets
Older Snippets »
1 total  XML / RSS feed