Thursday, December 7, 2023
HomeSoftware EngineeringGet all dates between two dates inclusive in Python

Get all dates between two dates inclusive in Python


If you wish to print out an inventory of all dates between 2 dates (a date vary), then you need to use the next 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 vary(delta.days + 1):
    day = start_date + timedelta(days=i)
    print(day)

This can outcome within the following vary:

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

To just remember to get the format you need again:

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments