Python – How to Get Current Time in Milliseconds datetimepythontime How do I get the current time in milliseconds in Python? Best Answer Using time.time(): import time def current_milli_time(): return round(time.time() * 1000) Then: >>> current_milli_time() 1378761833768 Related SolutionsPython Datetime – How to Get the Current Time in Python? Use datetime: >>> import datetime >>> now = datetime.datetime.now() >>> now datetime.datetime(2009, 1, 6, 15, 8, 24, 78915) >>> print(now) 2009-01-06 15:08:24.789150 For just the clock time without the date: >>> now.time() datetime.time(15, 8, 24, 78915) >>> print(now.time()) 15:08:24.789150 To save typing, you can import the datetime object from the datetime module: >>> from datetime import datetime Then remove the prefix datetime. from all of the above. Python Time Format Conversion – Milliseconds and Back From string to milliseconds: s = "4:34.234" hours, minutes, seconds = (["0", "0"] + s.split(":"))[-3:] hours = int(hours) minutes = int(minutes) seconds = float(seconds) miliseconds = int(3600000 * hours + 60000 * minutes + 1000 * seconds) From milliseonds to string: hours, milliseconds = divmod(miliseconds, 3600000) minutes, milliseconds = divmod(miliseconds, 60000) seconds = float(milliseconds) / 1000 s = "%i:%02i:%06.3f" % (hours, minutes, seconds) Related Question
Best Answer
Using
time.time()
:Then: