471,354 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

how to get next month string?

Hi,
Could someone help on how to use python to output the next month string like
this?

"AUG07", suppose now is July 2007.

I think also need to consider Dec 07 case, it is supposed to output as
below:
"JAN07".

datetime module seems not supporting the arithmatic operations, any hints?

Thanks in advance,

Yinghe Chen
Jul 24 '07 #1
6 10877
On Jul 24, 5:31 am, "Yinghe Chen" <yinghe.c...@jeppesen.comwrote:
Hi,
Could someone help on how to use python to output the next month string like
this?

"AUG07", suppose now is July 2007.
I usually find time and date answers somewhere in here:

http://pleac.sourceforge.net/pleac_p...sandtimes.html

rd

Jul 24 '07 #2
On Jul 24, 8:31 pm, "Yinghe Chen" <yinghe.c...@jeppesen.comwrote:
Hi,
Could someone help on how to use python to output the next month string like
this?

"AUG07", suppose now is July 2007.

I think also need to consider Dec 07 case, it is supposed to output as
below:
"JAN07".

datetime module seems not supporting the arithmatic operations, any hints?

Thanks in advance,

Yinghe Chen
>>import datetime
def nextmo(d):
.... mo = d.month
.... yr = d.year
.... mo += 1
.... if mo 12:
.... mo = 1
.... yr += 1
.... return datetime.date(yr, mo, 1).strftime('%b%y').upper()
....
>>nextmo(datetime.date(2007,7,15))
'AUG07'
>>nextmo(datetime.date(2007,12,15))
'JAN08'
>>>
Jul 24 '07 #3
On Tue, 2007-07-24 at 05:15 -0700, John Machin wrote:
On Jul 24, 8:31 pm, "Yinghe Chen" <yinghe.c...@jeppesen.comwrote:
Hi,
Could someone help on how to use python to output the next month string like
this?

"AUG07", suppose now is July 2007.

I think also need to consider Dec 07 case, it is supposed to output as
below:
"JAN07".

datetime module seems not supporting the arithmatic operations, any hints?

Thanks in advance,

Yinghe Chen
>import datetime
def nextmo(d):
... mo = d.month
... yr = d.year
... mo += 1
... if mo 12:
... mo = 1
... yr += 1
... return datetime.date(yr, mo, 1).strftime('%b%y').upper()
A more concise variant:
>>import datetime
def nextmo(d):
.... mo = d.month
.... yr = d.year
.... nm = datetime.date(yr,mo,1)+datetime.timedelta(days=31)
.... return nm.strftime('%b%y').upper()

Going 31 days from the first of any month will always get us into the
next month. The resulting day of the month will vary, but we're throwing
that away with strftime.

--
Carsten Haese
http://informixdb.sourceforge.net
Jul 24 '07 #4
On Jul 24, 11:16 pm, Carsten Haese <cars...@uniqsys.comwrote:
On Tue, 2007-07-24 at 05:15 -0700, John Machin wrote:
On Jul 24, 8:31 pm, "Yinghe Chen" <yinghe.c...@jeppesen.comwrote:
Hi,
Could someone help on how to use python to output the next month string like
this?
"AUG07", suppose now is July 2007.
I think also need to consider Dec 07 case, it is supposed to output as
below:
"JAN07".
datetime module seems not supporting the arithmatic operations, any hints?
Thanks in advance,
Yinghe Chen
>>import datetime
>>def nextmo(d):
... mo = d.month
... yr = d.year
... mo += 1
... if mo 12:
... mo = 1
... yr += 1
... return datetime.date(yr, mo, 1).strftime('%b%y').upper()

A more concise variant:>>import datetime
>def nextmo(d):

... mo = d.month
... yr = d.year
... nm = datetime.date(yr,mo,1)+datetime.timedelta(days=31)
... return nm.strftime('%b%y').upper()

Going 31 days from the first of any month will always get us into the
next month. The resulting day of the month will vary, but we're throwing
that away with strftime.
+1 for the "+ 31 days" trick

Sorry about the assembly language :-)

Here's an alternative for folks who prefer legibility:
>>def nextmo(d):
.... yr, mo = divmod(d.year * 12 + d.month, 12)
.... return datetime.date(yr, mo + 1, 1).strftime('%b%y').upper()

<:-)>
And for the other folks, one of these days I'll get around to writing
a PEP for the /% operator.
</:-)>
Jul 24 '07 #5
Sounds like a job for dateutil (http://labix.org/python-dateutil).
It's not a built in module (it's in the cheeseshop at least), but it
looks like it pretty much does exactly what you want. If you really
dont want to download anything, I suppose you could create something
from datetime's timedeltas which do support arithmetic operations.

Jul 25 '07 #6
John's method works perfectly, thanks all for your kind help.
"John Machin" <sj******@lexicon.netwrote in message
news:11**********************@d30g2000prg.googlegr oups.com...
On Jul 24, 8:31 pm, "Yinghe Chen" <yinghe.c...@jeppesen.comwrote:
>Hi,
Could someone help on how to use python to output the next month string
like
this?

"AUG07", suppose now is July 2007.

I think also need to consider Dec 07 case, it is supposed to output as
below:
"JAN07".

datetime module seems not supporting the arithmatic operations, any
hints?

Thanks in advance,

Yinghe Chen
>>>import datetime
def nextmo(d):
... mo = d.month
... yr = d.year
... mo += 1
... if mo 12:
... mo = 1
... yr += 1
... return datetime.date(yr, mo, 1).strftime('%b%y').upper()
...
>>>nextmo(datetime.date(2007,7,15))
'AUG07'
>>>nextmo(datetime.date(2007,12,15))
'JAN08'
>>>>

Jul 25 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by nasht | last post: by
10 posts views Thread by cover | last post: by
3 posts views Thread by One | last post: by
5 posts views Thread by Seb | last post: by

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.