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

Question

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:
111009

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


Answer

Use datetime.timedelta()

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

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