Introduction
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 = datetime.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 = datetime.now()
date2 = datetime(day=1, month=7, year=2021)
timedelta = date2 - date1
print(timedelta)
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 = datetime.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 30 days.
You can construct a time-frame for those 30 days, using timedelta
and add or subtract that from any other datetime
object:
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
from datetime import datetime, timedelta
now = datetime.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 on a moving window (such as the trailing 30 days).
Conclusion
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.