Skip to content
Advertisement

How can I get the start and end dates for each week?

week = datetime.date(2022,4,10).isocalendar()[1]

After finding the week, how can I get the start and end date of the week?

Advertisement

Answer

Python 3.6 version of rshepp’s answer:

from datetime import datetime, date

year, week, day = date(2022, 4, 10).isocalendar()

date_first = datetime.strptime(f'{year}{week}0', '%Y%U%w')
date_last = datetime.strptime(f'{year}{week}6', '%Y%U%w')

print(date_first, date_last, sep='n')

Output, Sunday-Saturday

2022-04-03 00:00:00
2022-04-09 00:00:00

date_first = datetime.strptime(f'{year}{week}1', '%Y%U%w')
date_last = datetime.strptime(f'{year}{week+1}0', '%Y%U%w')
print(date_first, date_last, sep='n')

Output, Monday-Sunday

2022-04-04 00:00:00
2022-04-10 00:00:00
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement