DateTime Format Stuff

Bradley D. Larson blarson at crary.com
Mon Dec 30 17:55:51 EST 2002


There is a strptime that parses the  string date and returns a tuple which you
could nest with strftime to get the desired result:

>>> import strptime
>>> import time
>>> t = '2002-12-25 09:25:02'
>>> tt = strptime.strptime(t,'%Y-%m-%d %H:%M:%S')
>>> print tt
(2002, 12, 25, 9, 25, 2, 0, 0, 0)
>>>time.strftime('%m/%d/%Y',tt)
12/25/2002

ie.    time.strftime('%m/%d/%Y',strptime.strptime(t,'%Y-%m-%d %H:%M:%S'))


at which point you can reformat it using the strftime.


Gerhard Haering wrote:

> * Kenneth Godee <Ken at perfect-image.com> [2002-12-30 12:04 -0700]:
> > I'm pulling a date from a database and is returned as a
> > tuple ie....
> >
> > (<DateTime object for '2002-11-26 00:00:00.00' at 82237f0>, 99999)
> >
> > and I can...
> >
> > print rows4[0][0] and get...
> > 2002-11-26 00:00:00.00
> >
> > or..
> >
> > str(rows4[0][0])
> > 2002-11-26 00:00:00.00
> >
> > My question is what's the easiest way to change
> > the format of the above output from a tuple
> > to something like 11/26/2002.
> > I have mxDateTime, time, etc., just can't be as hard
> > as I'm making it, All the time modules seem to
> > have a lot of functions that work great on system time
> > like....
> > strftime("%m/%d/%Y", localtime())
> > gives me what I want,but won't work on the tuple from
> > the database.
> >
> > or should I just re the above and custom make my own output?
> > Just figured there's some function I'm missing that makes this
> > very easy?
>
> The DateTime object has a strftime method, among others ;-)
>
> The first thing I do in such cases is
>
> >>> dir(d) # d is a DateTime object
> ['COMDate', 'Format', 'Gregorian', 'Julian', '__copy__', '__deepcopy__',
> 'absdate', 'absdays', 'abstime', 'absvalues', 'calendar', 'day', 'day_of_week',
> 'day_of_year', 'days_in_month', 'dst', 'gmticks', 'gmtime', 'gmtoffset',
> 'hour', 'is_leapyear', 'iso_week', 'jdn', 'localtime', 'minute', 'mjd',
> 'month', 'second', 'strftime', 'ticks', 'tjd', 'tjd_myriad', 'tuple', 'tz',
> 'year', 'yearoffset']
>
> in the interactive interpreter which usually saves reading the documentation of
> the module in question <wink>.
>
> Gerhard
> --
> Favourite database:             http://www.postgresql.org/
> Favourite programming language: http://www.python.org/
> Combine the two:                http://pypgsql.sf.net/
> Embedded database for Python:   http://pysqlite.sf.net/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
A non-text attachment was scrubbed...
Name: blarson.vcf
Type: text/x-vcard
Size: 400 bytes
Desc: Card for Bradley D. Larson
URL: <http://mail.python.org/pipermail/python-list/attachments/20021230/f842e426/attachment.vcf>


More information about the Python-list mailing list