Get all dates between two dates inclusive in Python

0 min read 118 words

If you want to print out a list of all dates between 2 dates (a date range), then you can use the following script:

from datetime import date, timedelta

start_date = date(2021, 5, 31)
end_date = date(2021, 7, 28)

delta = end_date - start_date

for i in range(delta.days + 1):
    day = start_date + timedelta(days=i)
    print(day)

This will result in the following range:

2021-05-31
2021-06-01
...
2021-07-27
2021-07-28

To make sure that you get the format you want back:

day.strftime("%Y-%m-%d")

This date manipulation technique is useful for many Python applications. If you’re interested in other Python challenges involving numerical sequences, you might want to check out getting the next biggest number with the same digits using Python .

Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags