Skip to content

Instantly share code, notes, and snippets.

@woods
Created July 20, 2011 17:32
Show Gist options
  • Save woods/1095440 to your computer and use it in GitHub Desktop.
Save woods/1095440 to your computer and use it in GitHub Desktop.
Calculating elapsed working days / hours
>> d1 = Date.new( 2006, 12, 1 )
=> #<Date: 4908141/2,0,2299161>
>> d2 = Date.new( 2007, 1, 15 )
=> #<Date: 4908231/2,0,2299161>
>> (d1..d2)
=> #<Date: 4908141/2,0,2299161>..#<Date: 4908231/2,0,2299161>
>> weekends = [0, 6]
=> [0, 6]
>> (d1..d2).each { |i| puts i }
<snip>
>> (d1..d2).each { |i| puts i.wday }
<snip>
>> (d1..d2).reject { |i| weekends.include? i.wday }.each { |i| puts i }
<snip>
>> (d1..d2).reject { |i| weekends.include? i.wday }.length
=> 32
>> (d1..d2).reject { |i| weekends.include? i.wday }.length * 8
=> 256
>> holidays = [Date.new(2006,1,1), Date.new(2007,12,25)]
=> [#<Date: 4908203/2,0,2299161>, #<Date: 4908189/2,0,2299161>]
>> (d1..d2).reject { |i| weekends.include?(i.wday) || holidays.include?(i) }.length * 8
=> 240
>> (d1..d2).reject { |i| weekends.include?(i.wday) || holidays.include?(i) }.length
=> 30
>> (Time.now.year..(Time.now.year+10)).each do |year|
?> holidays << Date.new(year, 1, 1)
>> holidays << Date.new(year, 12,25)
>> end
=> 2011..2021
>> holidays
<snip>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment