Get all dates between two dates inclusive in Python

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)
Code language: Python (python)

This will result in the following range:

2021-05-31 2021-06-01 ... 2021-07-27 2021-07-28
Code language: Bash (bash)

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

day.strftime("%Y-%m-%d")
Code language: Python (python)
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments