Created
May 26, 2011 03:35
-
-
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)
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 '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