Formatting "yesterday's" date in python


Question

I need to find "yesterday's" date in this format MMDDYY in Python.

So for instance, today's date would be represented like this: 111009

I can easily do this for today but I have trouble doing it automatically for "yesterday".

1
201
10/1/2015 2:39:51 PM

Accepted Answer

Use datetime.timedelta()

>>> from datetime import date, timedelta
>>> yesterday = date.today() - timedelta(days=1)
>>> yesterday.strftime('%m%d%y')
'110909'
337
5/11/2019 4:40:56 AM

from datetime import datetime, timedelta

yesterday = datetime.now() - timedelta(days=1)
yesterday.strftime('%m%d%y')

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon