1

Python datetime.now() gives me current time including milliseconds like this:

2014-08-22 19:23:40.630000

How do I convert the above datetime object to a string that includes the milliseconds?

I looked into time.strftime() but it does not provide an option for milliseconds.

Ideas?

Thanks.

snowcrash09
  • 4,694
  • 27
  • 45
Klaus Nji
  • 18,107
  • 29
  • 105
  • 185

1 Answers1

2

Use datetime.strftime. In Python 3.4.1:

from datetime import datetime
mytime= datetime.now()
s= mytime.strftime("%Y-%b-%d %H:%M:%S.%f")
print(s)
'2014-Aug-23 08:51:32.911271'
snowcrash09
  • 4,694
  • 27
  • 45