Fix Python – Formatting “yesterday’s” date in python


Asked By – y2k

I need to find “yesterday’s” date in this format MMDDYY in Python.

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

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

Now we will see solution for issue: Formatting “yesterday’s” date in python


Use datetime.timedelta()

>>> from datetime import date, timedelta
>>> yesterday = - timedelta(days=1)
>>> yesterday.strftime('%m%d%y')

This question is answered By – Jarret Hardie

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0