[Python] Get the previous month

Thing you want to do

--I want to add the previous month to the file name β†’ I want to get the previous month

point

--Get the current date with datetime and subtract one month --It seems easy to use dateutil

Sample code

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

Impressions

――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

[Python] Get the previous month
Calculate the previous month in Python
Python Note: Get the current month
Get last month in python
Get the desktop path in Python
Get the weather with Python requests
Get the weather with Python requests 2
Get the script path in Python
How to get the Python version
Get the desktop path in Python
Get the host name in Python
[Python] How to get the first and last days of the month
Get the last day of the specified month
[Python] Get the character code of the file
Get the EDINET code list in Python
[Python] Get the main color from the screenshot
Get the contents of git diff from python
[Python] Get the files in a folder with Python
Get the weather in Osaka via WebAPI (python)
[Python] Get / edit the scale label of the figure
[Python] Get the main topics of Yahoo News
Get the caller of a function in Python
Get the X Window System window title in Python
[Python] Get the last updated date of the website
How to get the files in the [Python] folder
[Python] Get the day of the week (English & Japanese)
Get the update date of the Python memo file.
[Python] Get environment variables
Get the GNOME version
[Python] Get Qiita trends
the zen of Python
[Python3] Get date diff
Get date in Python
Get date with python
[Python] Split the date
Get the MIME Type
Note: How to get the last day of the month with python (added the first day of the month)
python get current time
How to get the variable name itself in python
How to get the number of digits in Python
[Python] Get the official file path of the shortcut file (.lnk)
[Python] Get the text of the law from the e-GOV Law API
[Python] Get the numbers in the graph image with OCR
[python] Get the list of classes defined in the module
Get the return code of the Python script from bat
Get the result in dict format with Python psycopg2
Get the size (number of elements) of UnionFind in Python
Get the value selected in Selenium Python VBA pull-down
[Python] Get the list of ExifTags names of Pillow library
Get the operation status of JR West with Python
[Python] Get the number of views of all posted articles
Get the URL of the HTTP redirect destination in Python
Get YouTube Comments in Python
Get country code with python
Python --bitflyer Get asset balance
Try to get the function list of Python> os package
python3 Measure the processing speed.
Towards the retirement of Python2
Get your heart rate from the fitbit API in Python!
Download the file in Python
Find the difference in Python