Skip to content

Instantly share code, notes, and snippets.

@mkaschenko
Last active May 16, 2022 06:15
Show Gist options
  • Save mkaschenko/13a1d4a6942c4fd898600d681d91f03a to your computer and use it in GitHub Desktop.
Save mkaschenko/13a1d4a6942c4fd898600d681d91f03a to your computer and use it in GitHub Desktop.
The difference in years between dates
# Year 2017
module Date
def self.difference_in_years(left_date, right_date)
# NOTE: xxxx.yyzz (2017.1231)
#
# xxxx stands for years
# yy stands for months
# zz stands for days
#
# So that we could move the point on the date scale to determine the major grade (year).
diff_in_years = (right_date.year - left_date.year)
diff_in_months = (right_date.month - left_date.month) / 100.0
diff_in_days = (right_date.day - left_date.day) / 10_000.0
(diff_in_years + diff_in_months + diff_in_days).truncate
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment