Table of Contents
In Python, we can efficiently work with date and time values using the datetime
library. This library provides datetime
objects that can store such data. The dateutil
, arrow
libraries also allow us to process such values in Python.
Get All Dates Between Two Days in Python
In this tutorial, we will discuss how to get all dates between two days in Python. For this, we will take two datetime
objects and return all the dates between these objects.
Using the datetime.timedelta
Object
This method is very straight-forward. A timedelta
object is used to store and represent different durations of time. We store the total number of days between two dates in such an object.
Then we will use a for
loop and iterate till one more than the total days in this object. In the for
loop, we will add a day (represented using a timedelta
object) to the starting day and print it. Similarly, in the next iteration, the next day gets printed, and so on till the loop ends.
We implement this logic in the code below.
1 2 3 4 5 6 7 8 9 |
from datetime import date, timedelta d1 = date(2022, 10, 5) d2 = date(2022, 10, 15) d = d2-d1 for i in range(d.days + 1): day = d1 + timedelta(days=i) print(day) |
Output:
2022-10-06
2022-10-07
2022-10-08
2022-10-09
2022-10-10
2022-10-11
2022-10-12
2022-10-13
2022-10-14
2022-10-15
Using the pandas.date_range()
Function
The pandas
module is used to work with DataFrame
objects. Such objects frequently deal with date and time values and thus this module is equipped with functions that can operate on such values.
The date_range()
function from pandas
takes two dates and returns the total dates between these two dates including them both. We can use it to get all dates between two days in Python.
The object returned by this function can be iterated over using a for
loop to display the dates individually.
See the code below.
1 2 3 4 5 6 7 8 9 |
import pandas as pd from datetime import date d1 = date(2022, 10, 5) d2 = date(2022, 10, 15) d = pd.date_range(d1, d2) for i in d: print(i) |
Output:
2022-10-06 00:00:00
2022-10-07 00:00:00
2022-10-08 00:00:00
2022-10-09 00:00:00
2022-10-10 00:00:00
2022-10-11 00:00:00
2022-10-12 00:00:00
2022-10-13 00:00:00
2022-10-14 00:00:00
2022-10-15 00:00:00
Using the dateutil
Library
The dateutil
library provides an extension of functionalities and objects that can work with datetime
objects. The rrule
module of this library implements the recurrence of RFC. With such an object, we can calculate the range between two dates.
To get all dates between two days in Python, we will need to use the DAILY
frequency with the rrule()
constructor. We will iterate over the same and display all the dates.
See the following example.
1 2 3 4 5 6 7 8 |
from dateutil.rrule import rrule, DAILY from datetime import date d1 = date(2022, 10, 5) d2 = date(2022, 10, 15) for d in rrule(DAILY, dtstart=d1, until=d2): print(d) |
Output:
2022-10-06 00:00:00
2022-10-07 00:00:00
2022-10-08 00:00:00
2022-10-09 00:00:00
2022-10-10 00:00:00
2022-10-11 00:00:00
2022-10-12 00:00:00
2022-10-13 00:00:00
2022-10-14 00:00:00
2022-10-15 00:00:00
Using the numpy.arange()
Function
In Python, we use the numpy
library to create and work with array objects. The arange()
function from this library is used to create an array containing all values between two ranges.
We will pass the start and end dates in the function. We will create a timedelta
object of one day that will serve as the step
parameter.
We can iterate over the array and display the dates individually.
1 2 3 4 5 6 7 8 9 10 |
import numpy as np from datetime import date d1 = date(2022, 10, 5) d2 = date(2022, 10, 15) dt = timedelta(days = 1) days = np.arange(d1, d2, dt).astype(datetime) for d in days: print(d) |
Output:
2022-10-06 00:00:00
2022-10-07 00:00:00
2022-10-08 00:00:00
2022-10-09 00:00:00
2022-10-10 00:00:00
2022-10-11 00:00:00
2022-10-12 00:00:00
2022-10-13 00:00:00
2022-10-14 00:00:00
Conclusion
To conclude this article, we discussed several methods to get all dates between two days in Python. In the first method, we manually calculated the total days between two dates and use a timedelta
object to add one day to the starting date till the ending date.
In the other three methods, we created the range between the two dates and displayed the dates individually. For this, we used pandas.date_range()
function, the dateutil
library and the numpy.arange()
function respectively.