[Python](EN) Get dates between two datetime dates

Post about getting dates between two datetime dates


Environment and Prerequisite

  • Python


Example

  • Get dates between two datetime dates
import datetime
start_date = datetime.datetime(2021, 11, 15)
end_date = datetime.datetime(2021, 11, 21)

dates = [(start_date + datetime.timedelta(days=day_delta)) for day_delta in range((end_date - start_date).days + 1)]

for date in dates:
    print(date)
2021-11-15 00:00:00
2021-11-16 00:00:00
2021-11-17 00:00:00
2021-11-18 00:00:00
2021-11-19 00:00:00
2021-11-20 00:00:00
2021-11-21 00:00:00
  • Get formatted dates between two datetime dates
import datetime
start_date = datetime.datetime(2021, 11, 15)
end_date = datetime.datetime(2021, 11, 21)

dates = [(start_date + datetime.timedelta(days=day_delta)).strftime("%Y/%m/%d") for day_delta in range((end_date - start_date).days + 1)]

for date in dates:
    print(date)
2021/11/15
2021/11/16
2021/11/17
2021/11/18
2021/11/19
2021/11/20
2021/11/21


Reference