Ever wondered what date it’ll be 5 years from your wedding day? Or how many years and months old your pet is? You don’t need a clunky online tool—like a basic Years Calculator Online—Python can act as your go-to years calculator with just a few lines of code. Whether you’re adding time to a date, subtracting it, or finding the gap between two dates, this guide breaks it down in plain English.
First: Grab the Right Tools
Python has a built-in datetime module for basic dates, but it stumbles with years and months (since they’re not all the same length—looking at you, February!). For that, we’ll use python-dateutil, a free library that handles messy date math like a pro. Install it first:
pip install python-dateutil 1. Add Years/Months/Days to a Date
Let’s say you want to plan a 10-year reunion for your high school grad date (2014-06-18) or calculate a project deadline 6 months from now. relativedelta from dateutil makes this effortless.
Example: Plan a Future Date
Your high school graduation was on 2014-06-18. What date is your 10-year reunion?
from datetime import datetime
from dateutil.relativedelta import relativedelta
# Set the starting date (graduation)
grad_date = datetime(2014, 6, 18) # Format: year, month, day
# Add 10 years (we could add months=6 or days=15 too!)
reunion_date = grad_date + relativedelta(years=10)
# Print it nicely (YYYY-MM-DD format)
print("10-Year Reunion Date:", reunion_date.strftime("%Y-%m-%d")) # Output: 2024-06-18 Output:

Want to add multiple units? Just stack them: relativedelta(years=2, months=3, days=10) for 2 years, 3 months, and 10 days later.
2. Subtract Years/Months/Days from a Date
Need to find a past date? Maybe you want to know what date it was 2 years and 1 month before your child’s birthday (2020-09-05). Just flip the plus sign to minus.
Example: Find a Past Date
Your child was born on 2020-09-05. What date was it 2 years and 1 month before that?
from datetime import datetime
from dateutil.relativedelta import relativedelta
# Child's birthday
birthday = datetime(2020, 9, 5)
# Subtract 2 years and 1 month
past_date = birthday - relativedelta(years=2, months=1)
print("Past Date:", past_date.strftime("%Y-%m-%d")) # Output: 2018-08-05 Pro Tip: Don’t use the built-in timedelta for years/months! It only works for days/hours/minutes. relativedelta knows that 1 month after January 31 is February 28 (or 29 in leap years).
Output:

3. Calculate Time Between Two Dates (Years, Months, Days)
This is the most useful part—finding how many years, months, and days separate two dates. Think: calculating your age, or how long you’ve had your job.
Example 1: Calculate Your Age
You were born on 1998-03-22. What’s your age today?
from datetime import datetime
from dateutil.relativedelta import relativedelta
# Your birth date
birth_date = datetime(1998, 3, 22)
# Get today's date automatically
today = datetime.now()
# Find the difference
age = relativedelta(today, birth_date)
# Print detailed age
print(f"Age: {age.years} years, {age.months} months, {age.days} days")
# Example Output: Age: 26 years, 5 months, 10 days Output:

4. Work with Date Strings (Real-World Use Case)
Most of the time, dates come as strings (like "2023-12-31" or "31/12/2023") instead of typed objects. Use datetime.strptime() to convert them first—here’s how:
Example: Difference Between String Dates
Your first car was bought on "2015-05-10" and sold on "2023-09-25". How long did you own it?
from datetime import datetime
from dateutil.relativedelta import relativedelta
# Date strings (common YYYY-MM-DD format)
buy_date_str = "2015-05-10"
sell_date_str = "2023-09-25"
# Convert strings to datetime objects
# %Y = 4-digit year, %m = 2-digit month, %d = 2-digit day
buy_date = datetime.strptime(buy_date_str, "%Y-%m-%d")
sell_date = datetime.strptime(sell_date_str, "%Y-%m-%d")
# Calculate ownership time
ownership = relativedelta(sell_date, buy_date)
print(f"Owned for: {ownership.years} years, {ownership.months} months")
# Output: Owned for: 8 years, 4 months Different string format? Adjust the code:
- "31/12/2023" → use "%d/%m/%Y"
- "Dec 31, 2023" → use "%b %d, %Y"
FAQ: Common Python Years Calculator Hacks
relativedelta fixes this by changing it to 2023-02-28 automatically.date1 = datetime(2023, 1, 1)
date2 = datetime(2024, 1, 1)
total_days = (date2 - date1).days
print(total_days) # Output: 365 datetime handles time too. Just include it when creating the object: datetime(2024, 1, 1, 14, 30) (hour, minute).Wrap Up: Your Go-To Python Years Calculator
With datetime and python-dateutil, you’ve got a flexible calculator that does more than just count years. Add future dates, dig up past ones, or measure the time between moments—all with code you can copy-paste right now. No more guessing or using untrustworthy online tools.
Got a trickier date problem? Like handling timezones or excluding weekends? Drop a note and we’ll help you tweak the code!