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

Your email address will not be published. Required fields are marked *