-4
I have a function that receives a date of birth and another reference date to calculate the age of an individual in reference date. It should return me a list containing how many days, months and years respectively the (a) individual will have.
def calculaIdade(dtNascimento, dtLimite):
"""
Solução do @[Tomasz Zielinski] e @Williams
:param dtNascimento:
:param dtLimite:
:return: List[dias, meses, anos]
"""
print(f'dtNascimento ({type(dtNascimento)}): {dtNascimento}')
print(f'dtNascimento ({type(dtLimite)}): {dtLimite}')
idadeRelativa = relativedelta(dtNascimento, dtLimite)
return [idadeRelativa.days, idadeRelativa.months, idadeRelativa.years]
To that end, I’m using the library dateutil Python, as you can see in my code above. The problem is that the relativedelta method is returning me a very strange error:
Traceback (most recent call last):
File "/home/israeldev/Projetos/entrevistaController.py", line 273, in trocaTelaCentral
calculaAposentadoria = CalculosAposentadoria(self.processoModelo, self.clienteAtual, db=self.db)
File "/home/israeldev/Projetos/aposentadoria.py", line 56, in __init__
RegraTransicao.reducaoIdadeMinima: self.regraRedIdadeMinima(),
File "/home/israeldev/Projetos/aposentadoria.py", line 387, in regraRedIdadeMinima
idadeCliente = calculaIdade(dataNascimento, self.dibAtual)
File "/home/israeldev/Projetos/util/dateHelper.py", line 17, in calculaIdade
idadeRelativa = relativedelta(dtNascimento, dtLimite)
File "/home/israeldev/Projetos/lib/python3.9/site-packages/dateutil/relativedelta.py", line 114, in __init__
if not (isinstance(dt1, datetime.date) and
TypeError: isinstance() arg 2 must be a type or tuple of types
dtNascimento (<class 'datetime.datetime'>): 1968-03-28 00:00:00
dtNascimento (<class 'datetime.datetime'>): 2020-06-15 00:00:00
I’m using:
- Python 3.9.5
- Linux (Popos)
Just as a complement, I looked here in the related questions and found some things, but I did not succeed in my solution, probably because I did not understand very well the explanations I found... Below one of them...
The code you entered does not generate the given error: https://ideone.com/rn3kdr - maybe the problem lies elsewhere in the code that was not shown
– hkotsubo
Check whether the parameters
dtNascimento
anddtLimite
are of the typedatetime.datetime
.– Augusto Vasques