Oli Oli - 5 months ago 11
Python Question

Converting string into datetime

Short and simple. I've got a huge list of date-times like this as strings:

Jun 1 2005 1:33PM
Aug 28 1999 12:00AM


I'm going to be shoving these back into proper datetime fields in a database so I need to magic them into real datetime objects.

Any help (even if it's just a kick in the right direction) would be appreciated.

Edit: This is going through Django's ORM so I can't use SQL to do the conversion on insert.

Answer
from datetime import datetime

date_object = datetime.strptime('Jun 1 2005  1:33PM', '%b %d %Y %I:%M%p')

Link to the Python documentation for strptime

and a link for the strftime format mask