How to convert "2013-10-21 12:00:00.004" to datetime object in Python? The problem is there is decimal number in seconds.
Asked
Active
Viewed 100 times
-1
-
Why are the milliseconds a problem? It looks simple to parse. Can you show your code that parses the text. – David Heffernan Jan 10 '16 at 09:49
-
a = datetime.strptime("2013-10-21 12:00:00.004", "%y-%m-%d %H:%M:%S.%f") – user5025141 Jan 10 '16 at 09:54
-
And what happens when you do that? – David Heffernan Jan 10 '16 at 09:57
-
@user5025141 Your bug is in the %y, you need %Y as in my answer below – Yoav Glazner Jan 10 '16 at 10:00
-
Read this: https://docs.python.org/3.5/library/datetime.html#strftime-and-strptime-behavior Next time you ask a question make sure you include the full, but minimal, code necessary reproduce the problem, and the error message. Thanks. – David Heffernan Jan 10 '16 at 10:05
-
sorry, I will do that in the future. – user5025141 Jan 10 '16 at 10:10
-
sorry, I will do that in the future. – user5025141 Jan 10 '16 at 10:12
1 Answers
0
here is a working example
import datetime
dt = datetime.datetime.strptime("2013-10-21 12:00:00.004", "%Y-%m-%d %H:%M:%S.%f")
print (dt.microsecond)

Yoav Glazner
- 7,936
- 1
- 19
- 36
-
-
@David Heffernan It was still open when I started to write it I guess – Yoav Glazner Jan 10 '16 at 09:58