How to add one month to datetime?

Bengt Richter bokr at oz.net
Fri Oct 21 23:22:00 CEST 2005


On Fri, 21 Oct 2005 14:01:14 -0700, "Robert Brewer" <fumanchu at amor.org> wrote:

>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 =3D 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 =3D recur.sane_date(olddate.year, olddate.month + 1,
>olddate.day)
>
>will roll over any values which are out-of-bounds for their container.
>
>
The OP will still have to decide whether he likes the semantics ;-)
E.g., what does he really want as the date for "one month" after January 30 ?

Regards,
Bengt Richter



More information about the Python-list mailing list