The Python Oracle

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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3 Looping

--

Chapters
00:00 How Do I Get The Current Time In Milliseconds In Python?
00:12 Accepted Answer Score 1000
00:21 Answer 2 Score 177
00:37 Answer 3 Score 90
00:52 Answer 4 Score 59
00:58 Answer 5 Score 34
01:11 Thank you

--

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

--

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