Skip to content

Instantly share code, notes, and snippets.

@brandondrew
Created July 9, 2009 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandondrew/143393 to your computer and use it in GitHub Desktop.
Save brandondrew/143393 to your computer and use it in GitHub Desktop.
def date_to_epoch(date, offset)
Time.local(date.year, date.month, date.day + offset).to_i
end
# example:
# departure_date = Date.today
# days_until_return = 1
# puts date_to_epoch(departure_date, days_until_return)
def this_coming(day)
if day.is_a? Integer
day_number = day.to_i
elsif day.is_a? Symbol
day.to_s!
else day.is_a? String
days={"monday"=>1,'tuesday'=>2,'wednesday'=>3,'thursday'=>4,'friday'=>5,'saturday'=>6,'sunday'=>7}
day_number = days[day]
end
result = Date.today
result += 1 until result.wday == day_number
result
end
def next_week(day); end
def this_past(day); end
def last_week(day); end
puts this_coming('saturday') # this includes a saturday in the current week
puts next_week('saturday') # this does NOT include a saturday in the current week
puts this_past('tuesday') # this includes a tuesday in the current week
puts last_week('tuesday') # this does NOT includea tuesday in the current week
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment