Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert python datetime to timestamp in milliseconds

How to convert a human readable time with the format 20.12.2016 09:38:42,76 to Unix timestamps in milliseconds? I found a lot of similar questions, but didn't found this specific question/answer.

like image 631
Tom Avatar asked Jan 13 '17 13:01

Tom


People also ask

How do I convert time to milliseconds Python?

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.

How do I show milliseconds in Python?

strptime() function in python converts the string into DateTime objects. The strptime() is a class method that takes two arguments : string that should be converted to datetime object.

How do you convert microseconds to milliseconds Python?

To convert an amount of seconds to milliseconds, you can simply multiply (not divide) by 1000. This would return the integer 52000 .

How to convert a datetime string to milliseconds in Python?

How to convert a datetime string to millisecond UNIX time stamp? 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).

How to convert a DateTime object to milliseconds timestamp?

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. This will give the output −

How to get the timestamp in seconds in Python?

Give the date and time as parameters inside the datetime () function. Convert the datetime object into timestamp using datetime.timestamp () method. We will get the timestamp in seconds. Round off the timestamp and explicitly typecast the floating-point number into an integer to get the integer timestamp in seconds.

How to convert a DateTime object to string in Python?

Use the str () Function to Format DateTime to String The datetime module in Python allows us to create date and time objects easily manipulated and converted to different formats. This tutorial will cover how to convert a datetime object to a string containing the milliseconds. Use the strftime () Method to Format DateTime to String


Video Answer


2 Answers

In Python 3 this can be done in 2 steps:

  1. Convert timestring to datetime object
  2. Multiply the timestamp of the datetime object by 1000 to convert it to milliseconds.

For example like this:

from datetime import datetime  dt_obj = datetime.strptime('20.12.2016 09:38:42,76',                            '%d.%m.%Y %H:%M:%S,%f') millisec = dt_obj.timestamp() * 1000  print(millisec) 

Output:

1482223122760.0 

strptime accepts your timestring and a format string as input. The timestring (first argument) specifies what you actually want to convert to a datetime object. The format string (second argument) specifies the actual format of the string that you have passed.

Here is the explanation of the format specifiers from the official documentation:

  • %d - Day of the month as a zero-padded decimal number.
  • %m - Month as a zero-padded decimal number.
  • %Y - Year with century as a decimal number
  • %H - Hour (24-hour clock) as a zero-padded decimal number.
  • %M - Minute as a zero-padded decimal number.
  • %S - Second as a zero-padded decimal number.
  • %f - Microsecond as a decimal number, zero-padded on the left.
like image 97
Milo Avatar answered Oct 04 '22 21:10

Milo


For those who search for an answer without parsing and losing milliseconds, given dt_obj is a datetime:

python3 only, elegant

int(dt_obj.timestamp() * 1000) 

both python2 and python3 compatible:

import time  int(time.mktime(dt_obj.utctimetuple()) * 1000 + dt_obj.microsecond / 1000) 
like image 20
Ivan Klass Avatar answered Oct 04 '22 20:10

Ivan Klass