How to convert Python DateTime string into integer milliseconds?

PythonServer Side ProgrammingProgramming

You can get the current time in milliseconds in Python using the time module. You can get the time in seconds using time.time function(as a floating point value). To convert it to milliseconds, you need to multiply it with 1000 and round it off.

 example

import time
milliseconds = int(round(time.time() * 1000))
print(milliseconds)

Output

This will give the output −

1514825676008

If you want to convert a datetime object to milliseconds timestamp, you can use the timestamp function then apply the same math as above to get the milliseconds time. 

example

import time
from datetime import datetime
dt = datetime(2018, 1, 1)
milliseconds = int(round(dt.timestamp() * 1000))
print(milliseconds)

Output

This will give the output −

1514745000000
raja
Published on 08-Jan-2018 11:53:09
Advertisements