How to get the 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: 19-Feb-2020

586 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements