Add a month

N

novin01

Hi, this is probably a really simple question but...
How do you add a month to a datetime date in python? It would be nice
if you could do something like:

d = datetime.date(2006,2,17)
dm = datetime.timedelta(months=1)
d_new = d + dm

but timedelta doesn't have a 'months' setting. Am I missing some easy
method or do I have to code a work around myself??

Thanks in advance for the help!
 
R

Raymond Hettinger

[[email protected]]
Hi, this is probably a really simple question but...
How do you add a month to a datetime date in python? It would be nice
if you could do something like:

d = datetime.date(2006,2,17)
dm = datetime.timedelta(months=1)
d_new = d + dm

but timedelta doesn't have a 'months' setting. Am I missing some easy
method or do I have to code a work around myself??

You need to code you own version.

It was not included with datetime.timedelta() because the definition is
ambiguous (i.e. what date is one month after Jan 30th?).


Raymond
 
F

Fredrik Lundh

Hi, this is probably a really simple question but...
How do you add a month to a datetime date in python? It would be nice
if you could do something like:

d = datetime.date(2006,2,17)
dm = datetime.timedelta(months=1)
d_new = d + dm

but timedelta doesn't have a 'months' setting. Am I missing some easy
method or do I have to code a work around myself??

what do you expect d_new to be after the operation ? if the answer
is date(2006,3,17), what's date(2006,1,31) plus one month?

</F>
 
D

Daniel Dittmar

Fredrik said:
what do you expect d_new to be after the operation ? if the answer
is date(2006,3,17), what's date(2006,1,31) plus one month?

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: date domain error

in analogy to math.sqrt (-2)

Daniel
 
?

=?ISO-8859-1?Q?Gregory_Pi=F1ero?=

Here's how I do it:

def monthify(anint):
if anint%12==0:return 12
else:return anint%12

import datetime
d=datetime.datetime.today()
dplus1month=datetime.datetime(d.year,monthify(d.month+1),d.day)

We need monthify because adding 1 to 12 is bad otherwise!

-Greg
 
?

=?ISO-8859-1?Q?Gregory_Pi=F1ero?=

Actually, no wait, that's bad. It doesn't increment the year.

Does anyone have a simple way to code this?

-Greg
 
C

Carsten Haese

Actually, no wait, that's bad. It doesn't increment the year.

Does anyone have a simple way to code this?

-Greg

I don't know if this qualifies as simple, but it seems to work:

def addmonths(thedate,months):
"Add <months> months to <thedate>."
import datetime
y,m,d = thedate.timetuple()[:3]
y2, m2 = divmod(m+months-1, 12)
return datetime.date(y+y2,m2+1,d)

Note that the date constructor will raise an exception if the result
happens to be an invalid date such as February 30th or November 31st.

-Carsten
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,800
Messages
2,569,656
Members
45,399
Latest member
JettTancre
Top