[Python] Test the moon matagi of relative delta

It is a memo of relative delta (・ ω ・) Noshi

3/31 --What happens in a month? I was curious about things like that, so I tried it.

Source code (Python)

from datetime import datetime
from dateutil.relativedelta import relativedelta

dt_test1 = datetime(2020,3,31)
dt_test2 = dt_test1 - relativedelta(months=1)
dt_test3 = dt_test2 - relativedelta(months=1)
dt_test4 = dt_test3.replace(day=31)
dt_test5 = dt_test4 + relativedelta(months=1)

print("dt_test1={}, dt_test2={}, dt_test3={}, dt_test4={}, dt_test5={}"
.format(dt_test1, dt_test2, dt_test3, dt_test4, dt_test5))

Output result

dt_test1=2020-03-31 00:00:00, dt_test2=2020-02-29 00:00:00, dt_test3=2020-01-29 00:00:00, dt_test4=2020-01-31 00:00:00, dt_test5=2020-02-29 00:00:00

Commentary

variable What I wanted to try & results
dt_test1 First time(Leap year 3/31)
dt_test2 3/31-1 month=2/29 (2/31 does not exist so it can be rounded)
dt_test3 2/29-1 month=1/29 (It will not be the last day)
dt_test4 1/29 → 1/I just wanted to be 31
dt_test5 1/31+1 month=2/29 (It won't be March)

Impressions

I thought it would be convenient because it works intuitively for the purpose I want to use (・ ω ・) If you just want to add 30 days, you can use relative delta (days = 30).

Recommended Posts

[Python] Test the moon matagi of relative delta
the zen of Python
Towards the retirement of Python2
About the ease of Python
About the features of Python
The Power of Pandas: Python
Test the version of the argparse module
The story of Python and the story of NaN
[Python] The stumbling block of import
AtCoder: Python: Daddy the sample test.
Existence from the viewpoint of Python
pyenv-change the python version of virtualenv
Change the Python version of Homebrew
[Python] Understanding the potential_field_planning of Python Robotics
Review of the basics of Python (FizzBuzz)
About the basics list of Python basics
Learn the basics of Python ① Beginners
Let's measure the test coverage of pushed python code on GitHub.
Change the length of Python csv strings
Check the behavior of destructor in Python
[Python3] Understand the basics of Beautiful Soup
Write the test in a python docstring
Pass the path of the imported python module
The story of making Python an exe
Learning notes from the beginning of Python 1
Check the existence of the file with python
About the virtual environment of python version 3.7
[Python] Understand the content of error messages
[Python3] Rewrite the code object of the function
I didn't know the basics of Python
The result of installing python in Anaconda
Test & Debug Tips: Create a file of the specified size in Python
[Python] Try pydash of the Python version of lodash
[python] Checking the memory consumption of variables
Check the path of the Python imported module
The story of manipulating python global variables
[python] [meta] Is the type of python a type?
The basics of running NoxPlayer in Python
Pandas of the beginner, by the beginner, for the beginner [Python]
The Python project template I think of.
In search of the fastest FizzBuzz in Python
Python Basic Course (at the end of 15)
Set the process name of the Python program
[Python] Get the character code of the file
Python code to determine the monthly signal of a relative strength investment
The story of blackjack A processing (python)
Intuitively learn the reshape of Python np
Python Note: The secret role of commas
Learning notes from the beginning of Python 2
python setup.py test the code using multiprocess
Japanese translation: PEP 20 --The Zen of Python
[Python3] Understand the basics of file operations
Test whether the observed data follow the Poisson distribution (Test of the goodness of fit of the Poisson distribution by Python)
Get the contents of git diff from python
Output the number of CPU cores in Python
[Python] Read the source code of Bottle Part 2
Try the python version of emacs-org parser orgparse
Aggregate test results using the QualityForward Python library
The story of low learning costs for Python
[Python] Sort the list of pathlib.Path in natural sort
Prepare the execution environment of Python3 with Docker