
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Iterate Through a Range of Dates in Python
The range of dates is defined by the starting date, ending date, and all dates in between. In Python, we have some built?in functions such as datetime.date(), range(), timedelta(), and, delta() will be used to iterate through a range of dates.
Let's take an example of this.
The Starting Date, 26?06?2023
The Ending Date, 30?06?2023
Therefore, the final result of a range of dates becomes:
26?06?2023
27?06?2023
28?06?2023
29?06?2023
30?06?2023
Syntax
The following syntax is used in the examples-
datetime.date()
The date is an instance function of the datetime library of Python that returns date objects.
range()
The built?in function range() is defined by returning the order of numbers according to the given length.
timedelta()
The timedelta() is an in?built Python function that follows the library named datetime and returns the differences of dates.
Using while loop
In the following example, start importing the module datetime that will be used to set the dates. Next, initialize the two different dates in the respective variable that will be used to calculate the differences. Then using the while loop it set the condition based on <= operator to get the ranges of dates. Using the built?in function timedelta() with += operator it shows all the dates.
Example
import datetime start = datetime.date(2023, 6, 3) end = datetime.date(2023, 6, 4) res_date = start print("The range dates are:") while res_date <= end: print(res_date) res_date += datetime.timedelta(days=1)
Output
The range dates are: 2023-06-03 2023-06-04
Using List Comprehension
In the following example, begin the program with a datetime module that contains the information of dates. Then using list comprehension it finds the differences between two dates. This can be done by using timedelta function with an increasing number of days to start the date until the end date is reached.
Example
import datetime start = datetime.date(2023, 6, 16) end = datetime.date(2023, 6, 19) date_of_range = [start + datetime.timedelta(days=delta) for delta in range((end - start).days + 1)] print("The range dates are:") for res_date in date_of_range: print(res_date)
Output
The range dates are: 2023-06-16 2023-06-17 2023-06-18 2023-06-19
Using timedelta()
In the following example, we will use the for loop where variable delta iterates through both the initialization dates using a built?in function range(). Next, add the start date with timedelta() where it accepts the parameters? days=delta that returns the differences between two dates and generates the final result of the program.
Example
import datetime start = datetime.date(2023, 6, 1) end = datetime.date(2023, 6, 5) print("The range of dates:") # Calculate the differences using range() for delta in range((end - start).days + 1): result_date = start + datetime.timedelta(days=delta) print(result_date)
Output
The range of dates: 2023-06-01 2023-06-02 2023-06-03 2023-06-04 2023-06-05
Using Recursive function to generate the even dates in the range list
In the following example, the program uses datetime library that will be used to set the dates. Then using the recursive function it accepts two parameters to set the start and end date which will be used to work on a range of dates. Next, using while loop it set the difference condition based on <= operator and calculates the even date by using timedelta(). Moving ahead to initialize the start and end dates in the respective variable. Finally, using the for loop it iterates both the variables i.e. start_date and end_date to generate the even dates.
Example
import datetime # Function definition def range_date(start, end): res_date = start while res_date <= end: yield res_date res_date += datetime.timedelta(days=2) start_date = datetime.date(2023, 6, 10) end_date = datetime.date(2023, 6, 20) print("The range dates are-") for c_date in range_date(start_date, end_date): print(c_date)
Output
The range dates are- 2023-06-10 2023-06-12 2023-06-14 2023-06-16 2023-06-18 2023-06-20
Conclusion
The iteration of range of dates specifies the two variables i.e. start and end date. Using for loop it iterates the dates based on specific conditions and operations. Then use the timedelta() function to return all the dates between two variables. This type of program helps to build the mathematical formula based on searching.