How to add one month to datetime?

Robert Brewer fumanchu at amor.org
Fri Oct 21 17:01:14 EDT 2005


John W wrote:
> I have been trying to figure out how to
> easily add just one month to a datetime
> object? ...I was wondering if there is
> simple way of doing this with built in
> datetime object?

If you want the same day in the succeeding month, you can try:

    newdate = datetime.date(olddate.year, olddate.month + 1,
olddate.day)

...but as you can see, that will run into problems quickly. See the
"sane_date" function here:
http://projects.amor.org/misc/browser/recur.py for a more robust
solution, where:

    newdate = recur.sane_date(olddate.year, olddate.month + 1,
olddate.day)

will roll over any values which are out-of-bounds for their container.


Robert Brewer
System Architect
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list