DateTime Format Stuff

Gerhard Haering gerhard.haering at gmx.de
Mon Dec 30 14:20:18 EST 2002


* 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/




More information about the Python-list mailing list