How to convert a datetime string to millisecond UNIX time stamp?


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

Updated on: 19-Feb-2020

549 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements