Using %f with strftime() in Python to get microseconds


I'm trying to use strftime() to microsecond precision, which seems possible using %f (as stated here). However when I try the following code:

import time
import strftime from time

print strftime("%H:%M:%S.%f")

...I get the hour, the minutes and the seconds, but %f prints as %f, with no sign of the microseconds. I'm running Python 2.6.5 on Ubuntu, so it should be fine and %f should be supported (it's supported for 2.6 and above, as far as I know.)

9/25/2014 5:28:08 PM

Accepted Answer

You can use datetime's strftime function to get this. The problem is that time's strftime accepts a timetuple that does not carry microsecond information.

from datetime import datetime"%H:%M:%S.%f")

Should do the trick!

7/21/2017 8:20:42 AM

You are looking at the wrong documentation. The time module has different documentation.

You can use the datetime module strftime like this:

>>> from datetime import datetime
>>> now =
>>> now.strftime("%H:%M:%S.%f")

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow