d1 = datetime.strptime(self.current_date, "%Y-%m-%d")
d2 = datetime.strptime(self.dob, "%Y-%m-%d")
current_age = (d1 - d2).year
Running this code give the following error:
AttributeError: 'datetime.timedelta' object has no attribute 'year'
datetime' has no attribute 'datetime'. To resolve the problem, we just have to “import datetime” instead of “from datetime import datetime” as we did it above. Looks good.
timedelta Objects. A timedelta object represents a duration, the difference between two dates or times. class datetime. timedelta (days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
The DateTime value is then converted to a date value using the dt. date() function.
As per the docs (https://docs.python.org/3/library/datetime.html), a timedelta
counts days
, not years. So try something like (d1 - d2).days / 365.25
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With