[Answer]-Substract integer value from datetime field

1👍

Use:

date_joined - (datetime.timedelta(365) * age)

To subtract age years from date_joined.

Just a note that this is a good quick-and-dirty solution, but is not very robust. Consider the following output:

>>> datetime.datetime(2013, 12, 12) - datetime.timedelta(365) * 15
datetime.datetime(1998, 12, 16, 0, 0)

As you can see, because of leap years (years that actually have 366 days), the result is 4 days after the date we actually wanted. A more robust solution would be:

date_joined.replace(year = date_joined.year - age)

Leave a comment