How to get start and end of date of a week from a given date?
## # Python's program to get start and end of week from datetime import datetime, timedelta date_str = '2018-01-14' date_obj = datetime.strptime(date_str, '%Y-%m-%d') start_of_week = date_obj - timedelta(days=date_obj.weekday()) # Monday end_of_week = start_of_week + timedelta(days=6) # Sunday print(start_of_week) print(end_of_week)
Sample output of above program.
C:\programs\time>pep8 --first example18.py
C:\programs\time>python example18.py
2018-01-08 00:00:00
2018-01-14 00:00:00
C:\programs\time>
C:\programs\time>python example18.py
2018-01-08 00:00:00
2018-01-14 00:00:00
C:\programs\time>
2018-04-11T21:30:00+05:30
2018-04-11T21:30:00+05:30
Amit Arora
Amit Arora
Python Programming Tutorial
Python
Practical Solution