473,327 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 12315
[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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Finlay | last post by:
Hi Group I am designing a report that should show a meter reading for each month and the previous meter reading for the previous month. The months are text stored in a field tMonth. The...
6
by: Hasanain F. Esmail | last post by:
Hi all, I sincerly thank you all in advance for your help to solve this problem. I have been trying to find a solution to this problem for sometime now but have failed. I am working on a...
6
by: Tony Miller | last post by:
All I have an aggregate query using the function Month & Year on a datereceived field ie: TheYear: Year() TheMonth: Month() These are the group by fields to give me a Count on another field by...
4
by: Ronald Celis | last post by:
Hi, is there anyway to get the Number of days in a given month and Year in C# thanks Ronald Celis
6
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference...
0
by: larry | last post by:
I am in the process of rewriting one of my first PHP scripts, an event calendar, and wanted to share the code that is the core of the new calendar. My current/previous calendar processed data...
22
by: Stan | last post by:
I am working with Access 2003 on a computer running XP. I am new at using Access. I have a Db with a date field stored as mm/dd/yyyy. I need a Query that will prompt for the month, ie. 6 for...
19
by: edfialk | last post by:
Hi, does anyone happen to know of a script that would return the number of seconds in a month if I give it a month and a year? My python is a little weak, but if anyone could offer some...
6
by: Nkhosinathie | last post by:
hello guys,i've started with classes and i've been given this program below to program and also i will post the source code if you need it.this program reads as folllows. create a class called...
0
by: marlberg | last post by:
Platform: Windows2000, WindowsXP, Windows Vista, etc Language: C#, ASP.NET Pre-compiled Libraries: Enterprise Library 3.0 full I have a requirement to implement in and display in C# and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.