Created
October 30, 2012 19:38
-
-
Save kalmbach/3982490 to your computer and use it in GitHub Desktop.
Extending Date Class. WeekDay and Next WeekDay methods.
This file contains hidden or 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
class Date | |
# Extend Date Class with some custom methods | |
# Returns the name of the week day. | |
# | |
# Date.today.weekday | |
# -> 'Monday' | |
def weekday | |
self.strftime("%A") | |
end | |
# Returns the next weed day | |
# | |
# Date.today.next_weekday('Wednesday') | |
# -> next wednesday from today. | |
def next_weekday(day = nil) | |
if day.is_a? String | |
day ||= self.weekday | |
wday = Date::DAYNAMES.index(day) | |
if wday.present? | |
pad = ((wday - self.wday) % 7) | |
pad = 7 if pad == 0 | |
return self + pad.days | |
end | |
end | |
self # should return nil? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment