Skip to content

Instantly share code, notes, and snippets.

@hypomodern
Created March 18, 2010 15:16
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 hypomodern/336457 to your computer and use it in GitHub Desktop.
Save hypomodern/336457 to your computer and use it in GitHub Desktop.
# Need to find out how many days are in the current month?
# leverage the power of Date#<<(n), which "rewinds" the date by n months.
# it helpfully sets the day correctly if you rewind from the 31st, so rewind from 12/31.
days_in_current_month = ( Date.new(Time.now.year, 12, 31).to_date << ( 12 - Time.now.month ) ).day
# You could also make this a method call
def days_in_month(month_number)
( Date.new(Time.now.year, 12, 31).to_date << ( 12 - month_number ) ).day
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment