Python Forum

Full Version: Convert String to Datetime Object
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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!
not tested:
datetime.datetime.strptime(date_date, "%Y-%m-%d %H:%M:%S.%f")
(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