Convert timedelta to years?

186,380

Solution 1

You need more than a timedelta to tell how many years have passed; you also need to know the beginning (or ending) date. (It's a leap year thing.)

Your best bet is to use the dateutil.relativedelta object, but that's a 3rd party module. If you want to know the datetime that was n years from some date (defaulting to right now), you can do the following::

from dateutil.relativedelta import relativedelta

def yearsago(years, from_date=None):
    if from_date is None:
        from_date = datetime.now()
    return from_date - relativedelta(years=years)

If you'd rather stick with the standard library, the answer is a little more complex::

from datetime import datetime
def yearsago(years, from_date=None):
    if from_date is None:
        from_date = datetime.now()
    try:
        return from_date.replace(year=from_date.year - years)
    except ValueError:
        # Must be 2/29!
        assert from_date.month == 2 and from_date.day == 29 # can be removed
        return from_date.replace(month=2, day=28,
                                 year=from_date.year-years)

If it's 2/29, and 18 years ago there was no 2/29, this function will return 2/28. If you'd rather return 3/1, just change the last return statement to read::

    return from_date.replace(month=3, day=1,
                             year=from_date.year-years)

Your question originally said you wanted to know how many years it's been since some date. Assuming you want an integer number of years, you can guess based on 365.2425 days per year and then check using either of the yearsago functions defined above::

def num_years(begin, end=None):
    if end is None:
        end = datetime.now()
    num_years = int((end - begin).days / 365.2425)
    if begin > yearsago(num_years, end):
        return num_years - 1
    else:
        return num_years

Solution 2

If you're trying to check if someone is 18 years of age, using timedelta will not work correctly on some edge cases because of leap years. For example, someone born on January 1, 2000, will turn 18 exactly 6575 days later on January 1, 2018 (5 leap years included), but someone born on January 1, 2001, will turn 18 exactly 6574 days later on January 1, 2019 (4 leap years included). Thus, you if someone is exactly 6574 days old, you can't determine if they are 17 or 18 without knowing a little more information about their birthdate.

The correct way to do this is to calculate the age directly from the dates, by subtracting the two years, and then subtracting one if the current month/day precedes the birth month/day.

Solution 3

First off, at the most detailed level, the problem can't be solved exactly. Years vary in length, and there isn't a clear "right choice" for year length.

That said, get the difference in whatever units are "natural" (probably seconds) and divide by the ratio between that and years. E.g.

delta_in_days / (365.25)
delta_in_seconds / (365.25*24*60*60)

...or whatever. Stay away from months, since they are even less well defined than years.

Solution 4

Here's a updated DOB function, which calculates birthdays the same way humans do:

import datetime
import locale


# Source: https://en.wikipedia.org/wiki/February_29
PRE = [
    'US',
    'TW',
]
POST = [
    'GB',
    'HK',
]


def get_country():
    code, _ = locale.getlocale()
    try:
        return code.split('_')[1]
    except IndexError:
        raise Exception('Country cannot be ascertained from locale.')


def get_leap_birthday(year):
    country = get_country()
    if country in PRE:
        return datetime.date(year, 2, 28)
    elif country in POST:
        return datetime.date(year, 3, 1)
    else:
        raise Exception('It is unknown whether your country treats leap year '
                      + 'birthdays as being on the 28th of February or '
                      + 'the 1st of March. Please consult your country\'s '
                      + 'legal code for in order to ascertain an answer.')
def age(dob):
    today = datetime.date.today()
    years = today.year - dob.year

    try:
        birthday = datetime.date(today.year, dob.month, dob.day)
    except ValueError as e:
        if dob.month == 2 and dob.day == 29:
            birthday = get_leap_birthday(today.year)
        else:
            raise e

    if today < birthday:
        years -= 1
    return years

print(age(datetime.date(1988, 2, 29)))

Solution 5

Get the number of days, then divide by 365.2425 (the mean Gregorian year) for years. Divide by 30.436875 (the mean Gregorian month) for months.

Share:
186,380

Related videos on Youtube

Migol
Author by

Migol

My main interests are .NET Framework (C#-based only), Ruby, Internet technologies, utilizing open-source in big projects.

Updated on September 22, 2021

Comments

  • Migol
    Migol over 2 years

    I need to check if some number of years have been since some date. Currently I've got timedelta from datetime module and I don't know how to convert it to years.

  • Migol
    Migol about 15 years
    I'm really worried about leap years. It should check if person is over 18 years old.
  • Ehab Developer
    Ehab Developer about 15 years
    Then there's no easy one-liner, you're going to have to parse the two dates and figured out if the person has passed their 18th birthday or not.
  • John Machin
    John Machin over 14 years
    That is NOT what anybody means or uses when it's a question of how many years service or has a person attained a particular age.
  • John Machin
    John Machin over 14 years
    A person born on 29 February will be treated as having attained age 1 on the following 28 February.
  • John Mee
    John Mee over 14 years
    Ok. Corrected to accommodate the 0.08% of the population born on the 29th by inverting the test from "is birthday after today" to "is birthday before today". Does that solve it?
  • brianary
    brianary over 14 years
    You can be fully accurate with 365.2425 (instead of 365.25), which takes the 400 year exception into account for the Gregorian calendar.
  • brianary
    brianary over 14 years
    Your 365.25 should be 365.2425 to take the 400 year exception of the Gregorian calendar into account.
  • John Mee
    John Mee about 14 years
    It does work correctly for your example!?! If I set "today" to 28th February 2009, and the date-of-birth to 29th February 2008 it returns Zero- at least for me; not 1 as you suggest (Python 2.5.2). There's no need to rude Mr Machin. Exactly what two dates are you having trouble with?
  • John Machin
    John Machin about 14 years
    I'll try again: A person born on 29 February will be treated by most people for most legal purposes as having attained age 1 on the following 28 February. Your code produces 0, both before and after your "correction". In fact, the two versions of your code produce EXACTLY the same output for ALL 9 input possibilities (month < == > X day < == >). BTW, 100.0/(4*365+1) produces 0.068, not 0.08.
  • John Mee
    John Mee about 14 years
    Oh, well there you go. I didn't know the legalities and you failed to make them unambiguous. Sounds like it was better off the first time around. Unfortunately I'm not able to determine what your 9 scenarios are from the comment. But don't bother to explain; it is certainly time for you to share with us your solution; one that works the way you like.
  • John Machin
    John Machin about 14 years
    Sigh. (0) Addressing 29 February issues is essential in any date arithmetic; you just ignored it. (1) Your code wasn't better off first time; what don't you understand in "produce EXACTLY the same output"? (2) Three possible atomic results (<, ==, >) comparing today.month and dob.month; three possible atomic results comparing today.day and dob.day; 3 * 3 == 9 (3) stackoverflow.com/questions/2217488/…
  • Litherum
    Litherum over 13 years
    Well, the problem can be solved correctly - you can tell ahead of time which years have leap days and leap seconds and all that. It's just that there's no extremely elegant way of doing it without subtracting the years, then the months, then the days, etc... in the two formatted dates
  • Trey Hunner
    Trey Hunner about 11 years
    This breaks when dob is Feb. 29 and the current year is not a leap year.
  • antihero
    antihero over 10 years
    Your function legally breaks for countries such as the UK and Hong Kong, because they "round up" to the 1st of March for leap years.
  • Jblasco
    Jblasco over 10 years
    The leap year thing does not apply when the years are divisible by 100, except when they are divisible by 400. So, for year 2000: - it is divisible by four, so it should be leap but... - it is also divisible by a hundred, so it should not be leap but... - it is divisible by 400, so it was actually a leap year. For 1900: - it is divisible by 4 so it should be leap. - it is divisible by 100, so it should not be leap. - it is NOT divisible by 400, so this rule does not apply. 1900 was not a leap year.
  • jfs
    jfs over 8 years
  • gvoysey
    gvoysey over 7 years
    see also this and this Both are excellent lists of things that are not true about time.
  • Braiam
    Braiam almost 3 years
    This is the way... unless you work on any sector that likes guesstimates: health, geology, etc.
  • xyres
    xyres almost 3 years
    Subtracting just the years is such an elegant solution yet not very obvious.

Related