Python ProgrammingPython Programming

Getting the date of 7 days ago from current date in Python

from datetime import datetime, timedelta

now = datetime.now()

for x in range(7):
    d = now - timedelta(days=x)
    print(d.strftime("%Y-%m-%d"))
Output
2021-05-18
2021-05-17
2021-05-16
2021-05-15
2021-05-14
2021-05-13
2021-05-12