How to add months to a date (datetime object)?

CM cmpython at gmail.com
Sun Mar 15 17:47:19 EDT 2009


On Mar 15, 1:28 pm, tinn... at isbd.co.uk wrote:
> I have a date in the form of a datetime object and I want to add (for
> example) three months to it.  At the moment I can't see any very
> obvious way of doing this.  I need something like:-
>
>     myDate = datetime.date.today()
>     inc = datetime.timedelta(months=3)
>     myDate += inc
>
> but, of course, timedelta doesn't know about months. I had a look at
> the calendar object but that didn't seem to help much.
>
> --
> Chris Green

As someone pointed out, dateutil to the rescue:

>>> import datetime
>>> from dateutil.relativedelta import *

>>> now = datetime.date.today()
>>> now
datetime.date(2009, 3, 15)

>>> now+relativedelta(months=+3)
datetime.date(2009, 6, 15)

Che



More information about the Python-list mailing list