1

How do you go about the string '2014-03-16 18:28:11' back to datetime.datetime. I was wondering if there an easier way than the below

>>> datetime.datetime(2014, 3, 16, 18, 28, 11)
>>> str(datetime.datetime(2014, 3, 16, 18, 28, 11))
'2014-03-16 18:28:11'
>>> complete = str(datetime.datetime(2014, 3, 16, 18, 28, 11))
>>> date, time = complete.split(" ")
>>> [int(x) for x in date.split("-")]
[2014, 3, 16]
>>> [int(x) for x in time.split(":")]
[18, 28, 11]

Then use each variable as inputs to datetime.datetime

user1741339
  • 507
  • 2
  • 6
  • 11
  • possible duplicate of [how to convert a string date into datetime format in python?](http://stackoverflow.com/questions/19068269/how-to-convert-a-string-date-into-datetime-format-in-python) – DavidK Jul 24 '14 at 13:01

1 Answers1

4

You want datetime.strptime.

date = datetime.datetime.strptime(complete, '%Y-%m-%d %H:%M:%S')
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895