unconverted data remains: .387000 in Python
You are doing it backwards. Try this:
from datetime import datetime
mytime = "2015-02-16 10:36:41.387000"
myTime = datetime.strptime(mytime, "%Y-%m-%d %H:%M:%S.%f")
myFormat = "%Y-%m-%d %H:%M:%S"
print "Original", myTime
print "New", myTime.strftime(myFormat)
result:
Original 2015-02-16 10:36:41.387000
New 2015-02-16 10:36:41
You forgot to reference microseconds in myFormat
myFormat = "%Y-%m-%d %H:%M:%S.%f"
Anyway, you can convert it with less steps
from datetime import datetime
mytime = "2015-02-16 10:36:41.387000"
full = "%Y-%m-%d %H:%M:%S.%f"
myTime = datetime.strptime(mytime, full)
>>> datetime.datetime(2015, 2, 16, 10, 36, 41, 387000)
Here mytime
is in datetime
object. If you want print without microseconds, use the strftime
myfmt = "%Y-%m-%d %H:%M:%S"
print datetime.strptime(mytime, full).strftime(myfmt)
>>> 2015-02-16 10:36:41