[Fixed]-Show age from postgres database and then it's show days only

1👍

Function age returns interval datatype.

In your case: 26 years 3 Mons 7 days which is datetime.timedelta(9587)


You could use EXTRACT to get only years:

SELECT *,EXTRACT(YEAR FROM age(date_of_birth)) AS age 
...

0👍

I just ended up on this page while looking for something different. Although the question is four years old, I think one solution should perhaps be added: instead of calculating the date difference in the database, you could also use the built-in timesince Django template filter, which should give you an output similar to the string output of the PostgreSQL age() function, see also https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#timesince

Not sure when the filter was added, it definitely is available in Django 1.8 and 1.9.

👤goetz

Leave a comment