Python: give start and end of week data from a given date

Use the datetime module.

This will yield start and end of week (from Monday to Sunday):

from datetime import datetime, timedelta

day = '12/Oct/2013'
dt = datetime.strptime(day, '%d/%b/%Y')
start = dt - timedelta(days=dt.weekday())
end = start + timedelta(days=6)
print(start)
print(end)

EDIT:

print(start.strftime('%d/%b/%Y'))
print(end.strftime('%d/%b/%Y'))

Leave a Comment