How can I convert a Python DateTime string to milliseconds?

Python Methods and Functions

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


Example

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



Output

This will output &


 1514825676008 

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






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 







Tutorials