Skip to content

Instantly share code, notes, and snippets.

@tsommer
Created May 26, 2011 03:35
Show Gist options
  • Save tsommer/992500 to your computer and use it in GitHub Desktop.
Save tsommer/992500 to your computer and use it in GitHub Desktop.
Given today's date and a weekday find the date of that weekday in the future (or today)
require 'time'
DAYS = {
"Sun" => 0,
"Mon" => 1,
"Tue" => 2,
"Wed" => 3,
"Thu" => 4,
"Fri" => 5,
"Sat" => 6
}
DAY_IN_SECONDS = 24 * 60 * 60
def date_for_day(day)
now = Time.new
difference = DAYS[day] - now.wday
difference += 7 if difference < 0
now + difference * DAY_IN_SECONDS
end
p "Thu: " + date_for_day('Thu').to_s
p "Fri: " + date_for_day('Fri').to_s
p "Sat: " + date_for_day('Sat').to_s
p "Sun: " + date_for_day('Sun').to_s
p "Mon: " + date_for_day('Mon').to_s
p "Tue: " + date_for_day('Tue').to_s
p "Wed: " + date_for_day('Wed').to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment