On Tue, 2007-07-24 at 05:15 -0700, John Machin wrote: > On Jul 24, 8:31 pm, "Yinghe Chen" <[EMAIL PROTECTED]> wrote: > > 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 -- http://mail.python.org/mailman/listinfo/python-list