Search code examples
python-3.xdatetimepython-datetime

Python previous month with 2 digit


I want 04 as output in premonth . Can some one help on this? tried diff format and no luck.

enter code here
premonth = str(int(time.strftime('%m'))-1)

tried using python date of the previous month but due to strftime restriction I am not able to proceed.


Solution

  • Not the best way but this should work:

    a = str(int(time.strftime('%m'))-1)
    a = '0'+a if len(a)==1 else a