[Python] How to get the first and last days of the month

Conclusion

Easy to use the replace method.

from datetime import datetime
from dateutil.relativedelta import relativedelta

today = datetime.now().date()
print(today)
# => 2020-05-27

last_month_start = (today - relativedelta(months=1)).replace(day=1)
print(last_month_start)
# => 2020-04-01

last_month_end = today.replace(day=1) - relativedelta(days=1)
print(last_month_end)
# => 2020-04-30

Don't miss the variable name.

reference

https://www.lifewithpython.com/2018/06/python-get-first-and-last-day-of-month.html

Recommended Posts

[Python] How to get the first and last days of the month
Note: How to get the last day of the month with python (added the first day of the month)
Note: Get the first and last items of Python OrderedDict non-destructively
I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
How to get the number of digits in Python
How to get the Python version
How to get the last (last) value in a list in Python
Get the last day of the specified month
[Python] Get the last updated date of the website
How to get dictionary type elements of Python 2.7
How to get the files in the [Python] folder
[Python Kivy] How to get the file path by dragging and dropping
[Python] How to specify the window display position and size of matplotlib
How to get started with the 2020 Python project (windows wsl and mac standardization)
How to get the variable name itself in python
Get last month in python
[Python] Get the previous month
[Python] Summary of how to specify the color of the figure
Get the last element of the array by splitting the string in Python and PHP
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
[Blender] How to get the selection order of vertices, edges and faces of an object
Try to get the function list of Python> os package
[python] Summary of how to retrieve lists and dictionary elements
[Python] Summary of how to use split and join functions
Comparison of how to use higher-order functions in Python 2 and 3
Hook to the first import of the module and print the module path
How to get all the keys and values in the dictionary
How to get a list of built-in exceptions in python
I want to know the features of Python and pip
How to get into the python development environment with Vagrant
[Introduction to Python] How to get data with the listdir function
Overview of Python virtual environment and how to create it
Get the source of the page to load infinitely with python.
The story of Python and the story of NaN
First Python 3 ~ The beginning of repetition ~
How to get started with Python
[Python] How to import the library
Get the first element of queryset
Python Note: Get the current month
[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python
How to get the information of organizations, Cost Explorer of another AWS account with Lambda (python)
How to get and set the NTP server name by DHCP
How to determine the existence of a selenium element in Python
How to pass and study the Python 3 Engineer Certification Basic Exam
How to know the internal structure of an object in Python
python / pandas / dataframe / How to get the simplest row / column / index / column
How to get the ID of Type2Tag NXP NTAG213 with nfcpy
[Python] How to get divisors of natural numbers at high speed
[EC2] How to install chrome and the contents of each command
How to check the memory size of a variable in Python
[Introduction to Python] I compared the naming conventions of C # and Python.
First python ② Try to write code while examining the features of python
I want to output the beginning of the next month with Python
How to check the memory size of a dictionary in Python
How to get the vertex coordinates of a feature in ArcPy
The first artificial intelligence. How to check the version of Tensorflow installed.
Get and set the value of the dropdown menu using Python and Selenium
I summarized how to change the boot parameters of GRUB and GRUB2
Convert the result of python optparse to dict and utilize it
How to update the python version of Cloud Shell on GCP