Python Forum
Convert String to Datetime Object - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Convert String to Datetime Object (/thread-2929.html)



Convert String to Datetime Object - tkj80 - Apr-20-2017

Hi, 

I have the following

myList[0][0]
Output:
'02/28/2017 21:01:00'
type(myList[0][0])
Output:
<class 'str'>
str_date = myList[0][0]
from datetime import datetime
date_date = datetime.strftime(str_date, "%m/%d/%Y %H:%M:%S")
I tried to convert the string object to a datetime object but I got the following error message.

Error:
TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'str'
Shouldn't with datetime.strftime method, I would be able to convert the string object to datetime object?

Wonder if someone could please advise.

Thank you!


RE: Convert String to Datetime Object - Larz60+ - Apr-20-2017

not tested:
datetime.datetime.strptime(date_date, "%Y-%m-%d %H:%M:%S.%f")



RE: Convert String to Datetime Object - volcano63 - Apr-20-2017

(Apr-20-2017, 03:44 AM)tkj80 Wrote: Shouldn't with datetime.strftime method, I would be able to convert the string object to datetime object?

Wonder if someone could please advise.

Thank you!

No, strftime stands for string-from-time
strptime - I would hazard a guess - is string-pparse-to-time