Python: Get Number of Days Between Dates - Stack Abuse

Python: Get Number of Days Between Dates


In this tutorial, we'll take a look at how to get the number of days between two dates in Python.

We'll be using the built-in datetime package, that allows you to really easily work with datetime objects in Python.

Creating a Datetime Object

As datetime is a built-in module, you can access it right away by importing it at the top of your Python file.

You can construct datetime objects in a few different ways:

from datetime import datetime

date_string = "1 January, 2021"

now = # Create Datetime based on system clock
dt1 = datetime(day=1, month=1, year=2021) # Create Datetime with given arguments
dt2 = datetime.strptime(date_string, "%d %B, %Y") # Create Datetime from String

There are certain rules to follow when it comes to Converting Strings to Datetime in Python.

Get Number of Days Between Dates in Python

Trying to measure the numbers of days between dates without using the datetime module is a deceptively complex task - between accounting for leap years and the number of days in each month, trying to create your own implementation is nonsensical.

With datetime however, it becomes trivial.

You can simply subtract a date or datetime from each other, to get the number of days between them:

from datetime import datetime

date1 =
date2 = datetime(day=1, month=7, year=2021)

timedelta = date2 - date1

This returns a timedelta object, which contains days, seconds and microseconds and represents the duration between any two date/time or datetime objects.

Printing this object will return the days, hours, minutes, seconds and microseconds to that event:

149 days, 5:22:52.255124

If you're not interested in some of these metrics, you can specify which of them you'd like to access with timedelta.days, timedelta.seconds and timedelta.microseconds:

now =
new_years = datetime(day=1, month=1, year=2022)
countdown = new_years - now

print('Today is: ', now)
print('New Year is on: ', new_years)
print('Days until New Years: ', countdown.days)

Here, we've assigned the days between now and new_years into countdown, which is a timedelta object.

Then, we can simply access the days parameter of that object to get the number of days between them. This results in:

Today is:  2021-02-01 18:35:12.272524
New Year is on:  2022-01-01 00:00:00
Days until New Years:  333

Adding and Subtracting Days Using TimeDelta

What if instead of trying to subtract two known dates from each other, you wanted to add or subtract a time-frame? For example, a customer subscribed to your service for a monthly fee. You'll want to remind them to renew it after a 30 days.

Better understand your data with visualizations

  •  30-day no-questions refunds
  •  Beginner to Advanced
  •  Updated regularly (update June 2021)
  •  New bonus resources and guides

You can construct a time-frame for those 30 days, using timedelta and add or subtract that from any other datetime object:

from datetime import datetime, timedelta

now =
thirty_days = timedelta(days=30)

print('In 30 days: ', now + thirty_days)
print('30 days ago: ', now - thirty_days)

This results in:

In 30 days:  2021-03-03 18:41:49.922082
30 days ago:  2021-01-02 18:41:49.922082

This is an incredibly useful feature when trying to implement scheduling, or retrieving database entries based off a moving window (such as the trailing 30 days).


In this tutorial, we've covered everything you need to know about getting the number of days between two dates in Python.

We've worked with datetime objects and timedelta to achieve this functionality.

Last Updated: February 15th, 2021

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Spencer PorterAuthor

Full Stack Developer and Technical Writer.

I love writing about Python and React, and how anyone can create amazing products and tools.

Want a remote job?

    Prepping for an interview?

    • Improve your skills by solving one coding problem every day
    • Get the solutions the next morning via email
    • Practice on actual problems asked by top companies, like:

    Better understand your data with visualizations

    •  30-day no-questions refunds
    •  Beginner to Advanced
    •  Updated regularly (update June 2021)
    •  New bonus resources and guides

    © 2013-2021 Stack Abuse. All rights reserved.