Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kalmbach
Created October 30, 2012 19:38
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 kalmbach/3982490 to your computer and use it in GitHub Desktop.
Save kalmbach/3982490 to your computer and use it in GitHub Desktop.
Extending Date Class. WeekDay and Next WeekDay methods.
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