Like 201612
, I wanted to take the value of last month regardless of the date, so I wrote the code.
#Library import
import datetime
#Get today
today = datetime.datetime.today()
print today.strftime("%Y-%m-%d")
#Get the value for the first day of the month
thismonth = datetime.datetime(today.year, today.month, 1)
print thismonth.strftime("%Y-%m-%d")
#Get the value for the last day of the previous month
lastmonth = thismonth + datetime.timedelta(days=-1)
print lastmonth
#Specify the format and pick up only the year and month
print lastmonth.strftime("%Y%m")
It's a bit of a hassle, but I wanted to do it with just the default libraries that AWS Lambda can use.
Recommended Posts