The Python Oracle

How do I get the current time in milliseconds in Python?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Switch On Looping

--

Chapters
00:00 Question
00:18 Accepted answer (Score 951)
00:34 Answer 2 (Score 143)
00:57 Answer 3 (Score 95)
01:15 Answer 4 (Score 57)
01:26 Thank you

--

Full question
https://stackoverflow.com/questions/5998...

Accepted answer links:
[time.time()]: https://docs.python.org/3/library/time.h...

Answer 2 links:
[time.time_ns()]: https://docs.python.org/3/library/time.h...

Answer 3 links:
[time.time()]: https://docs.python.org/3/library/time.h...
[datetime]: https://docs.python.org/3/library/dateti...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #datetime #time

#avk47



ACCEPTED ANSWER

Score 1003


Using time.time():

import time

def current_milli_time():
    return round(time.time() * 1000)

Then:

>>> current_milli_time()
1378761833768



ANSWER 2

Score 91


time.time() may only give resolution to the second, the preferred approach for milliseconds is datetime.

from datetime import datetime
dt = datetime.now()
dt.microsecond



ANSWER 3

Score 59


def TimestampMillisec64():
    return int((datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)).total_seconds() * 1000) 



ANSWER 4

Score 34


Just sample code:

import time
timestamp = int(time.time()*1000.0)

Output: 1534343781311