Created
December 23, 2016 17:21
-
-
Save mattroyer/2aa412e4cd1bdb690b7b0d240ae1bb05 to your computer and use it in GitHub Desktop.
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
def start_of_next_week(date) | |
date + (7 - date.wday) | |
end | |
def date_of_next(day) | |
date = Date.parse(day) | |
delta = date > Date.today ? 0 : 7 | |
date + delta | |
end | |
def nth_day(day, now=Date.today) | |
day.gsub!(/\b(in|on|of)\b/, '') | |
day = day.split | |
nth = day.shift.to_i | |
first_day_name = day.first | |
first_day_num = %w(su mo tu we th fr sa).index(first_day_name[0..1].downcase) | |
month = %w(jan feb mar apr may jun jul aug sep oct nov dec).index(day.last[0..2].downcase) + 1 | |
first_day = Date.new(now.year, month) | |
first_occurance = first_day + ((first_day_num - first_day.wday) % 7) | |
nth == 1 ? first_occurance : first_occurance + (7 * (nth - 1)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment