-1

good morning, I'm in the middle of a project and I'm stuck on finding a way to return every day of the current week in a list. It would be something like this:

week=[ '2022-06-07' , '2022-06-08' , '2022-06-09' , '' , '...']

I would be very grateful if you could explain to me a way to obtain this result,,, thanks

ApyGuy
  • 13
  • 3

1 Answers1

0

You can shorten this but I wrote it out like this for clarity.

from datetime import datetime, timedelta

date_obj = datetime(2022, 6, 7)  # Today, Tuesday
monday = date_obj - timedelta(days=date_obj.weekday())  # assuming start of week is Monday
week = []
for i in range(7):
    week.append((monday + timedelta(days=i)).strftime("%Y-%m-%d"))
print(week)

Output:

# Monday 6/6 to Sunday 6/12
['2022-06-06', '2022-06-07', '2022-06-08', '2022-06-09', '2022-06-10', '2022-06-11', '2022-06-12']
mr_mooo_cow
  • 1,098
  • 1
  • 6
  • 16