Skip to content

Instantly share code, notes, and snippets.

@dmshvetsov
Last active August 29, 2015 14:16
Show Gist options
  • Save dmshvetsov/b3a9797bdf67ff3c7296 to your computer and use it in GitHub Desktop.
Save dmshvetsov/b3a9797bdf67ff3c7296 to your computer and use it in GitHub Desktop.
Collect only 'n' weekdays including current date, but no weekends
require 'date'
def next_n_work_days n
today = Date.today
number_of_weekends_days = ((n / 5) *2)
(1..(number_of_weekends_days + n) - 1).reduce([today]) do |days, plus_num_days|
day = today + plus_num_days
# or
# day = days.last.next
# and you don't have to use plus_num_days var
days << day if !day.saturday? and !day.sunday?
days
end
end
require 'date'
class Date
def weekend?
saturday? || sunday?
end
end
def weekdays (ffdays, startdate = Date.today)
(startdate..(startdate + ffdays)).select{ |day| !day.weekend? }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment