Pretty Print Relative Dates in Python
In this post, we will learn how to “pretty print” relative dates using the dateutil
library in Python. Specifically, given a relativedelta
object, we will print a string that represents the number of years, months, and days that have passed between two dates.
Introduction
I am working on a project called ManagerManager that helps me keep track of the people I work for. As my team has grown, it has become important to me to have a quick reminder to celebrate milestones such as work anniversaries.
PostgreSQL age Function
PostgreSQL has a useful built-in age function that allows you to get a human readable relative date from a date or datetime column.
For example, If I was in a database somewhere, I could use the age()
function to find out how old I am.
select name, birthday, age(birthday) from person;
name | birthday | age |
---|---|---|
Lev | 1988-06-09 | 31 years 11 mons 6 days |
Abstracting Away the SQL
In the first iteration of this project, I relied on “raw” SQL for all database operations. This made it easy to take advantage of the age()
function in PostgreSQL. As the project became more complex, working in SQL got out of hand.
I recently switched to SQLAlchemy to make working with the database simpler. Although SQLAlchemy allows you to call out to native database functions, I didn’t want to have a hard dependency on PostgreSQL for this application, so I started to search for a quick and dirty way to do the same thing in Python.
Exploring Python datetime
My initial investigation led me to the familiar datetime module in Python. This built-in module is great for manipulating date and time objects. It includes a timedelta
class which allows you to compare two datetimes at up to microseconds of resolution. This is generally useful, but unfortunately the maximum interval that this class provides is days.
import datetime
dob = datetime.date.fromisoformat('1988-06-09')
today = datetime.date.today()
today - dob
>>> datetime.timedelta(days=11662)
It turns out converting days to years, months, and weeks is much more complicated than I initially thought since you need to account for leap years and other nuances.
The dateutil Library
Luckily, the dateutil library provides several enhancements to the built-in datetime module. Most importantly for this use case, it has the relativedelta class which breaks the timedelta
into years, months, days, all the way down to microseconds.
import datetime
from dateutil.relativedelta import relativedelta
relativedelta(today, dob)
dob = datetime.date.fromisoformat('1988-06-09')
today = datetime.date.today()
>>> relativedelta(years=+31, months=+11, days=+6)
The only thing missing from dateutil.relativedelta
is a nice way to “pretty print” the result. For example, what if years, months, or days is equal to 0? I don’t want a string that says “0 years, 4 months, and 1 day”.
Ternary Operators to the Rescue
This was the first time that I’ve used a ternary operator in python and it actually feels like a good use case here.
def tenure(self):
rd = relativedelta(datetime.now(), self.start_date)
years = f'{rd.years} years, ' if rd.years > 0 else ''
months = f'{rd.months} months, ' if rd.months > 0 else ''
days = f'{rd.days} days' if rd.days > 0 else ''
return f'{years}{months}{days}'
A ternary operator is a shortcut to write an if/else
statement. In my opinion, it is useful for simple situations like this. Let’s break this down a little bit.
In the tenure()
function described above, the code for determining years
is equivalent to the following:
if years > 0:
years = f'{rd.years} years, '
else:
years = ''
Or, you could also write it like this:
years = ''
if years > 0:
years = f'{rd.years} years, '
However, I think the simple one-liner ternary operator makes the entire function a bit cleaner.
Going back to the birthday example, the final output of this function unsurprisingly looks like this:
31 years, 11 months, 6 days
Conclusion
To sum things up, we can use the dateutil
library coupled with your choice of if/else
syntax to provide a human readable way to show the difference between two dates.
Thank you for reading! Share your thoughts with me on bluesky, mastodon, or via email.
Check out some more stuff to read down below.
Most popular posts this month
- 2024
- Reinstalling Windows at 1am
- SQLite DB Migrations with PRAGMA user_version
- My Custom Miniflux CSS Theme
- How to Disable Wayland in Debian Testing
Recent Favorite Blog Posts
This is a collection of the last 8 posts that I bookmarked.
- Pluralistic: LLMs are slot-machines (16 Aug 2025) from Pluralistic: Daily links from Cory Doctorow
- Pluralistic: Bluesky creates the world's weirdest, hardest-to-understand binding arbitration clause (15 Aug 2025) from Pluralistic: Daily links from Cory Doctorow
- Just a Little More Context Bro, I Promise, and It’ll Fix Everything from Jim Nielsen’s Blog
- The Futzing Fraction from Deciphering Glyph
- Sit On Your Ass Web Development from Jim Nielsen’s Blog
- c10kday from daniel.haxx.se
- Pluralistic: AI's pogo-stick grift (02 Aug 2025) from Pluralistic: Daily links from Cory Doctorow
- Why don't smart watches use USB-C to recharge? from Terence Eden’s Blog
Articles from blogs I follow around the net
Pluralistic: Become unoptimizable (20 Aug 2025)
Today's links Become unoptimizable: Twiddle or be twiddled. Hey look at this: Delights to delectate. Object permanence: Penguins v Microsoft in the EU; Chastity belts are a joke; Austerity breeds Nazis, Yale says, "Prepare for death." Upcoming…
via Pluralistic: Daily links from Cory Doctorow August 20, 2025Theatre Review: Sluts With Consoles ★★★★⯪
Let's see if this post makes it through the spam filters! Sluts With Consoles is a brilliant two-hander. Girly-twirly pick-me Player One and Gothy just-one-of-the-boys Player Two are locked in mortal - and emotional - combat. They represent the duali…
via Terence Eden’s Blog August 20, 2025Embedding Wren in Hare
I’ve been on the lookout for a scripting language which can be neatly embedded into Hare programs. Perhaps the obvious candidate is Lua – but I’m not particularly enthusiastic about it. When I was evaluating the landscape of tools which are “like Lua, but no…
via Drew DeVault's blog August 20, 2025Generated by openring