How to Iterate over months between two dates in Python?
Last Updated :
28 Feb, 2023
In this article, we will discuss how to iterate over months between two dates using Python.
We can iterate over months between two dates using timedelta and rrule methods.
Method 1: Iteration using timedelta
timedelta() is used for calculating differences in dates and also can be used for date manipulations in Python
Example:
But using timedelta we can't iterate over months between dates perfectly because here we are adding 31 days for each month. But every month won't have exact 31 days. Some months have 30 and even 28, 29. In order to solve the problem rrule comes into the act that helps to iterate between dates by a specific period of time.
Code
Python3
# import necessary packages
from datetime import datetime, timedelta
# date initialisation
startDate = datetime(2020, 1, 10)
endDate = datetime(2020, 4, 20)
# stores 31 days that can be added
addDays = timedelta(days=31)
while startDate <= endDate:
print(startDate)
# add a month
startDate += addDays
Output2020-01-10 00:00:00
2020-02-10 00:00:00
2020-03-12 00:00:00
2020-04-12 00:00:00
Time complexity:O(1)
Space complexity:O(1)
Method 2: rrule
rrule is a package present in dateutil library and this package consists of a method also rrule which takes dtstart, until and specific time period as parameters which are start date, end date, and time period based on iteration respectively. Specific time periods are WEEKLY, MONTHLY, YEARLY, etc.
Note: Use MONTHLY when you want to iterate over months between dates.
Syntax:
rrule(rrule.MONTHLY, dtstart=start_date, until=end_date)
Example:
Iterate over months between dates using rrule.
Python3
# import necessary packages
from datetime import datetime
from dateutil import rrule
# dates
start_date = datetime(2021, 1, 1)
end_date = datetime(2022, 1, 1)
for dt in rrule.rrule(rrule.MONTHLY, dtstart=start_date, until=end_date):
print(dt)
Output2021-01-01 00:00:00
2021-02-01 00:00:00
2021-03-01 00:00:00
2021-04-01 00:00:00
2021-05-01 00:00:00
2021-06-01 00:00:00
2021-07-01 00:00:00
2021-08-01 00:00:00
2021-09-01 00:00:00
2021-10-01 00:00:00
2021-11-01 00:00:00
2021-12-01 00:00:00
2022-01-01 00:00:00
Example:
Iterate over years in between dates using rrule.
Python3
# import necessary packages
from datetime import datetime
from dateutil import rrule
# dates
start_date = datetime(2021, 1, 1)
end_date = datetime(2022, 1, 1)
for dt in rrule.rrule(rrule.YEARLY, dtstart=start_date, until=end_date):
print(dt)
Output2021-01-01 00:00:00
2022-01-01 00:00:00
Time complexity : O(years), where years is the number of years between start_date and end_date.
Space complexity : O(years), where years is the number of years between start_date and end_date
Similar Reads
Find number of months between two dates in R In this article, we will discuss how to Find the number of months between two dates in the R programming language. Example: Input: Date_1 = 2020/02/21  Date_2 = 2020/03/21 Output: 1 Explanation: In Date_1 and Date_2 have only one difference in month. Here we will use seq() function to get the resu
2 min read
How to generate month names as list in Python? Our task is to generate a Python list containing the names of all the months in the calendar from January to December, and our aim is to accomplish this efficiently. In this article, we will go through all possible approaches to achieve this task in Python. Generating Months Name as a ListA list is
3 min read
Add Months to datetime Object in Python In this article, let's delve into the techniques for Add Months to datetime Object in Python. Working with dates and times often requires manipulation and adjustment, and understanding how to add months to a datetime object is a crucial skill. We will explore various methods and libraries to achieve
3 min read
How to convert datetime to date in Python In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt.date f
3 min read
How to add Days to a Date in Python? Python provides an in-built module datetime which allows easy manipulation and modification of date and time values. It allows arithmetic operations as well as formatting the output obtained from the DateTime module. The module contains various classes like date, time, timedelta, etc. that simulate
2 min read