Skip to content

Instantly share code, notes, and snippets.

@mattroyer
Created December 23, 2016 17:21
Show Gist options
  • Save mattroyer/2aa412e4cd1bdb690b7b0d240ae1bb05 to your computer and use it in GitHub Desktop.
Save mattroyer/2aa412e4cd1bdb690b7b0d240ae1bb05 to your computer and use it in GitHub Desktop.
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