How to display age from dob obtained? Any suggestion?
models.py
from django.db import models
import datetime
dob = models.DateField(max_length=8)
age = models.IntegerField()
def __str__(self):
today = date.today()
age = today.year - dob.year
if today.month < dob.month or today.month == dob.month and today.day < dob.day:
age -= 1
return self.age
Thank you, Rads
You should use the python-dateutil to get the relativedelta
between dates:
from dateutil.relativedelta import relativedelta
from django.db import models
dob = models.DateField(max_length=8)
age = models.IntegerField()
def __str__(self):
today = date.today()
delta = relativedelta(today, self.dob)
return str(delta.years)
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