how to get next month string?

Y

Yinghe Chen

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
 
J

John Machin

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
.... 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()
....
 
C

Carsten Haese

... 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:.... 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.
 
J

John Machin

A more concise variant:>>> import datetime

... 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:
.... 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.
</:)>
 
S

Silfheed

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.
 
H

holdenweb

Hello. Is that you? Wondered what you had been up to lately and discovered that your email address hadn't transferred across to my new computer, so I had to resort to Google and found you on camp.lang.python - I hope!

regards
Steve
 

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
474,139
Messages
2,570,805
Members
47,351
Latest member
LolaD32479

Latest Threads

Top