Python ProgrammingPython Programming

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>