TECHIES WORLD

For Techs.... Techniques.... Technologies....

CpanelLinuxPython

How to get previous month in Python

Sometimes we need to get the previous month date for scripting purposes. We can use Python datetime module to acheive the same.

import datetime
today = datetime.date.today()
first = today.replace(day=1)
lastMonth = first - datetime.timedelta(days=1)
print(lastMonth.strftime("%Y%m"))

That's all…

Leave a Reply