The Python Oracle

How to increment datetime by custom months in python without using library

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Meadow

--

Chapters
00:00 Question
01:01 Accepted answer (Score 182)
01:51 Answer 2 (Score 574)
02:48 Answer 3 (Score 46)
03:05 Answer 4 (Score 18)
03:23 Thank you

--

Full question
https://stackoverflow.com/questions/4130...

Question links:
[relativedelta]: http://labix.org/python-dateutil
[strtotime]: http://pypi.python.org/pypi/timelib/0.2

Answer 1 links:
[dateutil's ]: http://labix.org/python-dateutil
[different meanings]: http://dateutil.readthedocs.org/en/lates...
[Add month value in python]: http://pythoninspire.blogspot.com/2013/0...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #datetime

#avk47



ANSWER 1

Score 609


This is short and sweet method to add a month to a date using dateutil's relativedelta.

from datetime import datetime
from dateutil.relativedelta import relativedelta
    
date_after_month = datetime.today()+ relativedelta(months=1)
print('Today: ',datetime.today().strftime('%d/%m/%Y'))
print('After Month:', date_after_month.strftime('%d/%m/%Y'))
Today:  01/03/2013

After Month: 01/04/2013

A word of warning: relativedelta(months=1) and relativedelta(month=1) have different meanings. Passing month=1 will replace the month in original date to January whereas passing months=1 will add one month to original date.

Note: this requires the python-dateutil module. Install with this command line:

pip install --user python-dateutil

Explanation : Add month value in python




ACCEPTED ANSWER

Score 190


Edit - based on your comment of dates being needed to be rounded down if there are fewer days in the next month, here is a solution:

import datetime
import calendar

def add_months(sourcedate, months):
    month = sourcedate.month - 1 + months
    year = sourcedate.year + month // 12
    month = month % 12 + 1
    day = min(sourcedate.day, calendar.monthrange(year,month)[1])
    return datetime.date(year, month, day)

In use:

>>> somedate = datetime.date.today()
>>> somedate
datetime.date(2010, 11, 9)
>>> add_months(somedate,1)
datetime.date(2010, 12, 9)
>>> add_months(somedate,23)
datetime.date(2012, 10, 9)
>>> otherdate = datetime.date(2010,10,31)
>>> add_months(otherdate,1)
datetime.date(2010, 11, 30)

Also, if you're not worried about hours, minutes and seconds you could use date rather than datetime. If you are worried about hours, minutes and seconds you need to modify my code to use datetime and copy hours, minutes and seconds from the source to the result.




ANSWER 3

Score 52


Here's my salt :

current = datetime.datetime(mydate.year, mydate.month, 1)
next_month = datetime.datetime(mydate.year + int(mydate.month / 12), ((mydate.month % 12) + 1), 1)

Quick and easy :)




ANSWER 4

Score 20


since no one suggested any solution, here is how i solved so far

year, month= divmod(mydate.month+1, 12)
if month == 0: 
      month = 12
      year = year -1
next_month = datetime.datetime(mydate.year + year, month, 1)