I would like to add one month to the specified date
import datetime dt = datetime.datetime(year=2014, month=5, day=2)
so i have to get
datetime.datetime(year=2014, month=6, day=2)
but with
dt = datetime.datetime(year=2015, month=1, day=31)
I have to get
datetime.datetime(year=2015, month=3, day=1)
because no 2015-02-31 (and I want my result to be within one day after )
Some months have 31 days, some others 30, about 29, some 28!
so adding datetime.timedelta is probably not very good (because we donβt know the number of days to add)
I noticed that Pandas has an interesting concept DateOffset
http://pandas.pydata.org/pandas-docs/stable/timeseries.html#dateoffset-objects
but I did not find the offset Month , just MonthBegin or MonthEnd
I also see this post. How to calculate the date six months from the current date using the Python datetime module?
so i tried dateutil.relativedelta but
from dateutil.relativedelta import relativedelta datetime.datetime(year=2015, month=1, day=31)+relativedelta(months=1)
returns
datetime.datetime(2015, 2, 28, 0, 0)
therefore, the result is rounded up one day before .
Is there a (clean) way to round the day after ?
edit: I gave an example with the addition of one month, but I also want to add, for example: 2 years and 6 months (using relativedelta(years=2, months=6) )