How to convert birthdate into an integer and count age in Django?

Katarzyna Antonik-Heller
3 min readApr 18, 2023

--

How to calculate age from birthdate in Django

I am building web app to track child’s activity in educational institution like nursery or school. One of my challenges was to calculate Child’s age from a given birthdate. Here is how it can be done.

How to calculate age from date of birth?

First, create Child model with an attribute “birth_date” as DateTimeField.

class Child(models.Model):
birth_date = models.DateTimeField()

Then create method connected with this model which will calculate age from given value. (Remember about import!)

from django.utils import timezone

class Child(models.Model):
full_name = models.CharField(max_length=100)
birth_date = models.DateTimeField()
parent = models.ForeignKey(Parent, on_delete=models.CASCADE, related_name="child")

@property
def age(self):
today = timezone.now().date()
age = int(
today.year
- (self.birth_date.year)
- ((today.month, today.day) < (self.birth_date.month, self.birth_date.day))
)
return age

def __str__(self):
return self.full_name

The method:

  • gets the current year using today.year, which is an instance of the date class from the datetime module
  • then it subtracts the birth year (from the birth_date attribute) of the object, using self.birth_date.year.

This is how we get overall information. Next we need to decide if a Child reached this “annual” age or not yet.

*Why @property decorator?

I used @property, because age need to be added as model’s attribute. The @property decorator — as a built-in Python part — allows to define a method as a "getter" for a class attribute. When you access the attribute, the getter method is called, and value is returned instead of the attribute itself.

In the context of Django, the @property decorator can be used to define computed properties on a Django model. This is the reason why I can easily access it in ModelAdmin and while creating corresponding serializer.

Age based on birth date and current date

Back to logic. In first part, we compare years from two given values, for example: 2023–2017 = 6.

...
age = int(today.year - (self.birth_date.year)
...

But it is tricky. Depending on what month do we currently have — children born in 2017 may not reached their birthday. It means, we need to check whether the condition is met.

This is why we need second part.

age = int(
today.year
- (self.birth_date.year)
- ((today.month, today.day) < (self.birth_date.month, self.birth_date.day))
)

If the current month and day are less than the birth month and day, then the child has not yet had their birthday this year and we need to subtract 1 from their age.

This is done using the comparison operator <(self.birth_date.month, self.birth_date.day) which returns a boolean value indicating whether the current month and day is less than the birth month and day.

Child model with age method

This is how designed model looks in Django admin panel:

Birth date in Child model

Because I am using Django REST Framework, I created serializer for this model with explicitly defined fields (in this case “age” — I can access it as attribute because of usage @property decorator), then view and url.

And this is how age method call result of a Child model instance is on detail page view.

Link to full repository.

--

--