--I want to add the previous month to the file name β I want to get the previous month
--Get the current date with datetime
and subtract one month
--It seems easy to use dateutil
First, install `` `dateutil```
pip install python-dateutil
Get today's date and try it for a month
tesy.py
#today
today = datetime.date.today()
#last month
sengetsu = today - relativedelta(months=1)
result
2020-03-09
I want to use `` `yyyyMM``` for the file name, so format it
test.Fix py
sengetsu = (today - relativedelta(months=1)).strftime('%Y%m')
print(sengetsu)
result
202003
ββIt turned out to be unexpectedly troublesome
--If it is `powershell```, it ends with
(get-date) .AddMonths (-1) .ToString ('yyyyMM')
`` in one line.
Recommended Posts