473,382 Members | 1,576 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,382 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 11487
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: nasht | last post by:
Hi, Interesting question: A user fills a form about how often an event happen. He has 4 fields: . In any of them, he can enter "*" as a joker, to say it doesn't matter. For example, his...
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
8
by: tfsmag | last post by:
i need to create a date range based on the current "shown" month on a calendar control to query a database and populate a datagrid based on that date range. how can i retrieve the "shown" month...
10
by: cover | last post by:
I can get the current month to display in a text box by inserting value="<? print strftime("%B"); ?>"> into the code for that particular text box on a form but how can I populate a drop down with...
2
by: DavidOwens | last post by:
SELECT DISTINCTROW Format(PERIOD.START_DT,"mmmm yyyy") AS MonthYear, SALES.PERIOD_ID, Sum(SALES.SALES) AS SumOfSALES, Sum(SALES.SALES_UNITS) AS SumOfSALES_UNITS, Sum(SALES.SALES_VALUE) AS...
25
by: rekhasc | last post by:
hi...... Its very urgent.............. how can i take only the year and month part and it should save in the database in the month and year field separately......... when i enter the date in the...
2
by: ncsthbell | last post by:
I am having problems getting the end date to calculate correctly. I start with Quarter '03/02', (YY/QTR), for this it means it is for the 2nd qtr of 2003. My goal is to get the begin & end dates...
3
by: One | last post by:
Hi group - I want to do a SELECT based on a date range - but mySQL syntax expects the month range to be two digits. So I have converted the month to show the leading zero like this : ...
5
by: Seb | last post by:
I want to count activity in a given month. I'm trying to do so with the linq code below however it reports: Error 1 'a' is inaccessible due to its protection level var ActivityByMonths = from a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.