converting string to a date format

MRAB python at mrabarnett.plus.com
Sun Dec 20 21:46:55 EST 2009


tekion wrote:
> All,
> I know there is a datetime module for converting and manipulate date
> format.  I have this  string date format: 24/Nov/2009:10:39:03 -0500
> and would like to convert it to a date format of "2009-11-24
> 10:39:03".  At the moment I am reading datetime module trying to find
> out if I could do it with datetime module.  Does any one know of a way
> besides slashing my way through it using string split function?

Use datetime.datetime.strptime() to parse the string; you'll need to
remove the timezone from the string first. The format you need to
provide is the same one you'd use if you were creating the string from a
datetime. Then call the .strftime() method of the resulting datetime
object, providing the appropriate format to create the new string.



More information about the Python-list mailing list