Last active
August 29, 2015 14:16
-
-
Save dmshvetsov/b3a9797bdf67ff3c7296 to your computer and use it in GitHub Desktop.
Collect only 'n' weekdays including current date, but no weekends
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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