JavaScript
x
2
1
week = datetime.date(2022,4,10).isocalendar()[1]
2
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:
JavaScript
1
9
1
from datetime import datetime, date
2
3
year, week, day = date(2022, 4, 10).isocalendar()
4
5
date_first = datetime.strptime(f'{year}{week}0', '%Y%U%w')
6
date_last = datetime.strptime(f'{year}{week}6', '%Y%U%w')
7
8
print(date_first, date_last, sep='n')
9
Output, Sunday-Saturday
JavaScript
1
3
1
2022-04-03 00:00:00
2
2022-04-09 00:00:00
3
JavaScript
1
4
1
date_first = datetime.strptime(f'{year}{week}1', '%Y%U%w')
2
date_last = datetime.strptime(f'{year}{week+1}0', '%Y%U%w')
3
print(date_first, date_last, sep='n')
4
Output, Monday-Sunday
JavaScript
1
3
1
2022-04-04 00:00:00
2
2022-04-10 00:00:00
3