I am using Django and trying to calculate the average client age, and for some reason this answer is escaping me. What I want to do is to loop through all clients in a group and get the mean delta of today and their dob.
Here is what the model looks like:
class Client (models.model):
...
dob = models.DateField(
null = False
)
So that I am trying to do something like this:
def age(self,qrydtm=date.today())
# here is a method (from funcs.py) that returns a list of dob for the group
delta =
delta_m = ((qrydate.year - self.dob.year)*12)+(qrydate.month - self.dob.month)
years = delta_m // 12
weeks = delta // 7
months = (delta_m - (years*12))
if (years > 0):
return ('%s yr - %s mn' % (years,months))
else:
return ('%s wk' % (weeks))
The method I am using, creates a list of dob like this [(datetime.date(1976, 8, 4),), (datetime.date(2005, 8, 8),), (datetime.date(1986, 10, 14),)]
This will get you the average age as a timedelta:
Edit:
With l being potentielly large I avoided the second list: