[Python] 두 datetime 사이의 날짜들 가져오기

두 datetime 사이의 날짜들을 가져오자


환경

  • Python


예제

  • 두 datetime 사이의 날짜들 가져오기
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
  • 두 datetime 사이의 날짜들을 포맷 변경해서 가져오기
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


참고자료