I have a str
variable which is my_variable = str(201705)
.
I want to get the previous month of my_variable
using datetime in python. Is there a method to accomplish this? So far I have:
from datetime import datetime
my_variable = str(201705)
month = datetime.strptime(my_variable, '%Y%m').strftime("%b%Y")
This gives me my current variables month in str format, so May2017. I want to get just the previous month stored in a new variable. So my_new_month
would = April.
Is this possible using the python datetime library, and if not, is there another solution to accomplish this?