import time
time.time() * 1000
where 1000 is milliseconds per second. If all you want is hundredths of a second since the epoch, multiply by 100.
Answer from nmichaels on Stack Overflowimport time
time.time() * 1000
where 1000 is milliseconds per second. If all you want is hundredths of a second since the epoch, multiply by 100.
In Python, datetime.now() might produce a value with more precision than time.time():
from datetime import datetime, timezone, timedelta
now = datetime.now(timezone.utc)
epoch = datetime(1970, 1, 1, tzinfo=timezone.utc) # use POSIX epoch
posix_timestamp_millis = (now - epoch) // timedelta(milliseconds=1) # or `/ 1e3` for float
In theory, time.gmtime(0) (the epoch used by time.time()) may be different from 1970.
Convert python datetime to timestamp in milliseconds - Stack Overflow
datetime - How do I get the current time in milliseconds in Python? - Stack Overflow
Converting unix timestamps in python
How do I create a datetime in Python from milliseconds? - Stack Overflow
Videos
In Python 3 this can be done in 2 steps:
- Convert timestring to
datetimeobject - Multiply the timestamp of the
datetimeobject 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 to 6 digits.
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)
Using time.time():
import time
def current_milli_time():
return round(time.time() * 1000)
Then:
>>> current_milli_time()
1378761833768
For Python 3.7+, time.time_ns() gives the time passed in nanoseconds since the epoch.
This gives time in milliseconds as an integer:
import time
ms = time.time_ns() // 1_000_000
Hi I'm trying to figure out how to convert unix timestamps. My code just looks like this right now but I'm getting the following error. Anybody know what I'm doing wrong?
import datetime timestamp = 1729322547223 dt_object = datetime.datetime.fromtimestamp(timestamp) print(dt_object) Traceback (most recent call last): File "c:\Users\elieh\OneDrive\Documents\friendsgg\main.py", line 21, in <module> dt_object = datetime.datetime.fromtimestamp(timestamp) OSError: [Errno 22] Invalid argument
A quick solution would be:
t=time.time()
millis = int((t - int(t))*1000)
As you said, the problem is that time doesn't necessarily give you the precision you want[1]. datetime would be a better option:
from datetime import datetime
now = datetime.utcnow() # or datetime.now(your_timezone)
formatted = now.strftime("%Y-%m-%d %H:%M:%S.%f")
print(formatted)
[1] Both in python 2.x and 3.x, according to the docs:
Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.