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

Steve Holden steve at holdenweb.com
Mon Mar 16 00:17:20 CET 2009


Roy Smith wrote:
> In article <49bd3ab8$0$510$bed64819 at news.gradwell.net>, tinnews 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.
> 
> Well, before you can "add three months" to something, you need to explain 
> what that means.
> 
> What is Nov 29th plus 3 months?
> 
> What is Jan 31st plus 3 months?
> 
> Months are different lengths.  Asking to "add 3 months" is kind of like 
> asking, "If I'm somewhere in the continental US (east of the Mississippi 
> River) and move three states to the west, how many miles have I moved?"
> --
> http://mail.python.org/mailman/listinfo/python-list
> 
OK, suppose I sign a contract on Nov 30 that requires me to make
quarterly payments. When is my next payment due?

The use case doesn't seem that unreasonable to me.

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
Holden Web LLC                 http://www.holdenweb.com/
Want to know? Come to PyCon - soon! http://us.pycon.org/




More information about the Python-list mailing list