I was having this need to convert string to datetime in python and I found that this is the easiest and cleanest way to do it:
from datetime import datetime
def string_to_datetime(string, time_format="%Y-%m-%d %H:%M:%S"):
#function to convert string to datetime
How to use it? As simple as:
The default format is suitable for string formatted like this “2012-03-24 12:34:55″. For more information about how to create your own time format, you can visit python official website.
There is an easier way to do this, Matt Schinckel suggested to use this piece of code and yes, it works: