Home > Enterprise >  I need to find the difference in years between today and a list of dates
I need to find the difference in years between today and a list of dates

Time:01-06

I am trying to write a simple function to compare a list of dates to today in order to find dates that are more than 13 years old. For the first part of the challenge I’m just trying to write the function that will compare the dates to find the difference today and each in the birthdays list.

import datetime

birthdays = [
    datetime.datetime(2012, 4, 29),
    datetime.datetime(2006, 8, 9),
    datetime.datetime(1978, 5, 16),
    datetime.datetime(1981, 8, 15),
    datetime.datetime(2001, 7, 4),
    datetime.datetime(1999, 12, 30)
]

today = datetime.datetime.today()


def is_over_13(dt):
    Diff = (today - dt)
    Return (diff)

Is_over_13(birthdays)

The problem I seem to be facing is that I’m trying to compare a datetime object to a list. So my thinking is that I need to be able to do something to the list to make it compatible to compare? Or the other way round.

I’m learning Python and this is step 1 of a specific code challenge so I can’t use lambda or panda or libraries. I’m expected to do it using functional chaining or comprehensions.

Thanks in advance

CodePudding user response:

I think you need a loop.

for dt in birthdays:
    diff=(today-dt)
return diff

CodePudding user response:

You can loop over a list of datetimes to all compare them one by one with today. This function will return True if one date is over 13 years.

def is_it_over_13(birthday_list):
    for date in birthday_list:
        if abs(today - date) > datetime.timedelta(days = 13*365):
            return True
        else:
            return False

CodePudding user response:

import datetime

birthdays = [
    datetime.datetime(2012, 4, 29),
    datetime.datetime(2006, 8, 9),
    datetime.datetime(1978, 5, 16),
    datetime.datetime(1981, 8, 15),
    datetime.datetime(2001, 7, 4),
    datetime.datetime(1999, 12, 30)
]

def is_over_13(dt):
    today = datetime.datetime.today()
    diff = (datetime.datetime.now() - dt).days
    # 4745 == 13*365
    if diff>4745:
        return dt

for i in birthdays:
    print(is_over_13(i))

CodePudding user response:

You have a few tweaks to makes.

import datetime

birthdays = [
    datetime.datetime(2012, 4, 29),
    datetime.datetime(2006, 8, 9),
    datetime.datetime(1978, 5, 16),
    datetime.datetime(1981, 8, 15),
    datetime.datetime(2001, 7, 4),
    datetime.datetime(1999, 12, 30)
]

today = datetime.datetime.today()


def is_over_13(dt):
    diff = (today - dt) # you have capital D, need to match variable name with lower case.
    return diff  # Return should be return
    
for birthday in birthdays:  # loop over each day to check
    print(is_over_13(birthday))  ## you have capital I, needs to match your function name with lower case i

Also your function name says is_over_13 which implies true or false return i think. You might want to take the diff value and compare it to 13 years and return true or false

This might look like

import datetime

birthdays = [
    datetime.datetime(2012, 4, 29),
    datetime.datetime(2006, 8, 9),
    datetime.datetime(1978, 5, 16),
    datetime.datetime(1981, 8, 15),
    datetime.datetime(2001, 7, 4),
    datetime.datetime(1999, 12, 30)
]

today = datetime.datetime.today()
DAYS_IN_YEAR = 365
YEARS = 13

def is_over_13(dt):
    diff = (today - dt).days # get the days for your diff
    return diff > (DAYS_IN_YEAR * YEARS)  # return if greater than 13 or not

for birthday in birthdays:  
    print(is_over_13(birthday))  

CodePudding user response:

This will get you started. You need a loop. In this case, I used a comprehension:

import datetime

birthdays = [
    datetime.datetime(2012, 4, 29),
    datetime.datetime(2006, 8, 9),
    datetime.datetime(1978, 5, 16),
    datetime.datetime(1981, 8, 15),
    datetime.datetime(2001, 7, 4),
    datetime.datetime(1999, 12, 30)
]

today = datetime.datetime.today()

>>> [f'{today.date()} - {bd.date()}>13: {((today-bd).days)>365.2425*13}' for bd in birthdays]
['2022-01-05 - 2012-04-29>13: False', '2022-01-05 - 2006-08-09>13: True', '2022-01-05 - 1978-05-16>13: True', '2022-01-05 - 1981-08-15>13: True', '2022-01-05 - 2001-07-04>13: True', '2022-01-05 - 1999-12-30>13: True']

Because of leap years, time differences are expressed in days, minutes, seconds. This uses a common, but less than perfect, shortcut of estimating a year difference by multiplying a day difference by 365.2425 that is usually accurate but not 100%.

To get 100% accuracy, you need to detect leap year birthdays and adjust the date to 2/28/the_year in a non-leap year.

To do that, use this function:

def is_older_than(bd, age, day=datetime.datetime.today()):
    try:
        age_date=bd.replace(year=bd.year age)
    except ValueError:
        age_date=bd.replace(year=bd.year age, day=28)
        
    return age_date<=day 

Then redo the loop that will be accurate by the day no matter what:

>>> [f'{today.date()} - {bd.date()}>13: {is_older_than(bd, 13)}' for bd in birthdays]
['2022-01-05 - 2012-04-29>13: False', '2022-01-05 - 2006-08-09>13: True', '2022-01-05 - 1978-05-16>13: True', '2022-01-05 - 1981-08-15>13: True', '2022-01-05 - 2001-07-04>13: True', '2022-01-05 - 1999-12-30>13: True']
  •  Tags:  
  • Related