Get Python Date Object for Last Wednesday



You can get the Python date object for last wednesday using some Python date math. Whatever the day of the week it is today, subtracting 2 from it and taking the modulus of the result by 7 will give us how back was wedenesday. 

example

from datetime import date
from datetime import timedelta
today = date.today()
offset = (today.weekday() - 2) % 7
last_wednesday = today - timedelta(days=offset)

Output

This will give you the output −

2017-12-27
Updated on: 2020-02-19T09:33:33+05:30

883 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements