converting string to a date format

T

tekion

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? Thanks.
 
M

MRAB

tekion said:
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.
 
T

tekion

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
 
M

MRAB

tekion said:
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.
 
T

tekion

tekionwrote:





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.

Thanks. This is what I have:

time.strftime("%Y-%m-%d %H:%M:%S", time.strptime("24/Nov/
2009:12:00:00", "%d/%b/%Y:%H:%M:%S")

and it seems to work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top