Reputation: 263
Sunday is first day the week.
E.g. Today is Saturday, 9th of Feb 2019. I want the date of first day of that week which is 3rd of Feb 2019. How can I do that in Python 3?
Also if today is 7th Feb 2019 which is Thursday, and the first of day of that week is also 3rd Feb 2019.
How can I do that in Python 3?
Thank you
Upvotes: 0
Views: 3611
Reputation: 41
You will want to use the datetime
module.
This will yield start of the week:
from datetime import datetime, timedelta
day = '09/Feb/2019'
dt = datetime.strptime(day, '%d/%b/%Y')
start = dt - timedelta(days=dt.weekday()+1)
print(start.strftime('%d/%b/%Y'))
Answer above modified from this answer on a similar question. The datetime
module is your friend!
Upvotes: 2