469,362 Members | 2,302 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

Add a month

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!

Feb 17 '06 #1
9 12098
[no*****@gmail.com]
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

Feb 17 '06 #2
no*****@gmail.comn wrote:
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>

Feb 17 '06 #3
Fredrik Lundh wrote:
no*****@gmail.comn wrote:
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?


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

in analogy to math.sqrt (-2)

Daniel
Feb 17 '06 #4
Fredrik Lundh wrote:

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?


February 31st, of course:

http://sql-info.de/mysql/gotchas.html#1_14

;-)

Paul

Feb 17 '06 #5
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.mo nth+1),d.day)

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

-Greg

On 17 Feb 2006 04:15:39 -0800, Paul Boddie <pa**@boddie.org.uk> wrote:
Fredrik Lundh wrote:

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?


February 31st, of course:

http://sql-info.de/mysql/gotchas.html#1_14

;-)

Paul

--
http://mail.python.org/mailman/listinfo/python-list

--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)
Feb 17 '06 #6
Actually, no wait, that's bad. It doesn't increment the year.

Does anyone have a simple way to code this?

-Greg
On 2/17/06, Gregory Piñero <gr********@gmail.com> wrote:
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.mo nth+1),d.day)

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

-Greg

On 17 Feb 2006 04:15:39 -0800, Paul Boddie <pa**@boddie.org.uk> wrote:
Fredrik Lundh wrote:

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?


February 31st, of course:

http://sql-info.de/mysql/gotchas.html#1_14

;-)

Paul

--
http://mail.python.org/mailman/listinfo/python-list

--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)

--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)
Feb 17 '06 #7
On Fri, 2006-02-17 at 16:10, Gregory Piñero wrote:
Actually, no wait, that's bad. It doesn't increment the year.

Does anyone have a simple way to code this?

-Greg
On 2/17/06, Gregory Piñero <gr********@gmail.com> wrote:
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.mo nth+1),d.day)

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

-Greg

On 17 Feb 2006 04:15:39 -0800, Paul Boddie <pa**@boddie.org.uk> wrote:
Fredrik Lundh wrote:
>
> 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?

February 31st, of course:

http://sql-info.de/mysql/gotchas.html#1_14

;-)

Paul

--
http://mail.python.org/mailman/listinfo/python-list

--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)



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
Feb 17 '06 #8
Paul Boddie napisa³(a):
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?


February 31st, of course:

http://sql-info.de/mysql/gotchas.html#1_14


MS SQL Server documentation marks dateadd() result as non-deterministic
in Transact-SQL reference. Adding 1 month to jan 31st gives feb 28 or
29, depending on year. It's not that bad, providing you know this
behaviour. ;)

--
Jarek Zgoda
http://jpa.berlios.de/
Feb 17 '06 #9
Ok, 30 minutes later, here's my best solution.

http://www.answermysearches.com/inde...-in-python/53/

(Posted on my website to make sure tabs are kept.)

Would someone mind double-checking my logic before I put this into
production next Tuesday?

-Greg
On 2/17/06, Gregory Piñero <gr********@gmail.com> wrote:
Actually, no wait, that's bad. It doesn't increment the year.

Does anyone have a simple way to code this?

-Greg
On 2/17/06, Gregory Piñero <gr********@gmail.com> wrote:
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.mo nth+1),d.day)

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

-Greg

On 17 Feb 2006 04:15:39 -0800, Paul Boddie <pa**@boddie.org.uk> wrote:
Fredrik Lundh wrote:
>
> 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?

February 31st, of course:

http://sql-info.de/mysql/gotchas.html#1_14

;-)

Paul

--
http://mail.python.org/mailman/listinfo/python-list

--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)

--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)

--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)
Feb 17 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Hasanain F. Esmail | last post: by
4 posts views Thread by Ronald Celis | last post: by
6 posts views Thread by Ante Perkovic | last post: by
reply views Thread by larry | last post: by
22 posts views Thread by Stan | last post: by
19 posts views Thread by edfialk | last post: by
6 posts views Thread by Nkhosinathie | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.