Skip to content

Instantly share code, notes, and snippets.

@epid
Created February 11, 2010 22:07
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 epid/302020 to your computer and use it in GitHub Desktop.
Save epid/302020 to your computer and use it in GitHub Desktop.
Misc. date functions in ruby
# Calculate the number of months between two dates
def months_between (start_date=Time.now, end_date=Time.now)
(end_date.month - start_date.month) + 12 * (end_date.year - start_date.year)
end
# Calculate a person's age
def age_at(date, dob)
day_diff = date.day - dob.day
month_diff = date.month - dob.month - (day_diff < 0 ? 1 : 0)
date.year - dob.year - (month_diff < 0 ? 1 : 0)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment