[Tutor] <type 'datetime.time'> Variable to String?

Kent Johnson kent37 at tds.net
Sat Feb 14 01:06:06 CET 2009


On Fri, Feb 13, 2009 at 6:50 PM, Wayne Watson
<sierra_mtnview at sbcglobal.net> wrote:
> That's pretty much the question in Subject. I've got a date time variable
> with, for example, 15:00:00 in hh:mm:ss format, and I'd like to make it a
> string.

Lots of choices:
In [1]: from datetime import datetime
In [2]: d=datetime.now()
In [4]: str(d)
Out[4]: '2009-02-13 19:03:43.517789'

In [6]: d.isoformat()
Out[6]: '2009-02-13T19:03:43.517789'

strftime() takes a format string so you can get pretty much whatever
format you want:
In [7]: d.strftime("%a, %d %b %Y %H:%M:%S +0000")
Out[7]: 'Fri, 13 Feb 2009 19:03:43 +0000'

Kent


More information about the Tutor mailing list