Python

Python 日付の差分で日付のリストを作る

from datetime import datetime as dt
from datetime import timedelta

start_date = dt.strptime("2021-01-01", '%Y-%m-%d')
end_date = dt.strptime("2021-01-05", '%Y-%m-%d')

days = (end_date - start_date).days + 1 # +1は最終日を含めるため

date_list = []
for i in range(days):
    date_list.append(start_date + timedelta(days=i))

for d in date_list:
    print(d.strftime("%Y-%m-%d"))
    # 2021-01-01
    # 2021-01-02
    # 2021-01-03
    # 2021-01-04
    # 2021-01-05