converting string to a date format

MRAB python at mrabarnett.plus.com
Mon Dec 21 15:05:52 EST 2009


tekion wrote:
> Ben,
> I do not have python 2.6 install, my version of Python is 2.4.
> Because of my version of Python I believe I have to perform what you
> have suggested:
> 
> This should, ideally, consist of two separate operations:
> 
>   * parse the string, using a specific format, to create a ‘datetime’
>     object
> 
>   * create a string representation of the datetime using your
> preferred
>     string format
> 
> So I guess I am stuck on parsing the string "24/Nov/2009:12:00:00
> -0500" using regex and or string function to get the output to
> "2009-11-24 12:00:00".  It looks like I may have to use regex to
> accomplish this and also re-map Nov to "11".  Does any one have any
> idea that would take "24/Nov/2009:HH:MM:SS" and format it to
> "2009-11-24 HH:MM:SS"? Thanks

If you don't have the 'datetime' module then you can use the 'time'
instead. Use time.strptime() to parse the string and time.strftime() to
create the new string.



More information about the Python-list mailing list