Last active
June 6, 2023 16:23
-
-
Save Lockyy/f927cc0f98ae84fc8b4341f6029c8d71 to your computer and use it in GitHub Desktop.
Monkey patch onto Date/Time/DateTime objects to allow them to be converted to an age, accommodating for leap years
This file contains 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
def to_age | |
today = Time.current | |
year_diff = today.year - year | |
today_is_past_birthday = today.month > month || (today.month == month && today.day >= day) | |
today_is_past_birthday ? year_diff : year_diff - 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment