Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

RE: How to add one month to datetime?

1 view
Skip to first unread message

Robert Brewer

unread,
Oct 21, 2005, 5:01:14 PM10/21/05
to John W, pytho...@python.org
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
fuma...@amor.org

Bengt Richter

unread,
Oct 21, 2005, 5:22:00 PM10/21/05
to
On Fri, 21 Oct 2005 14:01:14 -0700, "Robert Brewer" <fuma...@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

del...@ml1.net

unread,
Oct 21, 2005, 9:26:00 PM10/21/05
to
Bengt Richter wrote:
> 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

When dealing with bonds a common convention is to return the same day
in the succeeding month if it is valid otherwise return the last day of
that month. However, if the original date is the last day of the month,
then the last day of the succeeding month should always be returned.

This snippet adds an arbitrary number of months (positive or negative)
according to that convention.

OneDay = datetime.timedelta(days=1)

def addMonth(date, n=1):
# add n+1 months to date then subtract 1 day
# to get eom, last day of target month
q,r = divmod(date.month+n, 12)
eom = datetime.date(date.year+q, r+1, 1) - OneDay
if date.month != (date+OneDay).month or date.day >= eom.day:
return eom
return eom.replace(day=date.day)

Regards,
John Dell'Aquila

0 new messages