Skip to content Skip to sidebar Skip to footer

Can Not Convert 13 Digit Unix Timestamp In Python

Still new to this. I have tried to convert a 13 digit timestamp to something that you can read but to no luck. What i am doing is sampling temperature data from a building automati

Solution 1:

#!python2

# Epoch time needs to be converted to a human readable format.
# Also, epoch time uses 10 digits, yours has 13, the last 3 are milliseconds

import datetime, time

epoch_time = 1520912901432

# truncate last 3 digits because they're milliseconds
epoch_time = str(epoch_time)[0: 10]

# print timestamp without milliseconds
print datetime.datetime.fromtimestamp(float(epoch_time)).strftime('%m/%d/%Y -- %H:%M:%S')
'''
03/12/2018 -- 21:48:21
'''

# %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %

# If you need milliseconds
epoch_time = 1520912901432

# get first 10 digits
leading = str(epoch_time)[0: 10]

# get last 3 digits
trailing = str(epoch_time)[-3:]

# print timestamp with milliseconds
print datetime.datetime.fromtimestamp(float(leading)).strftime('%m/%d/%Y -- %H:%M:%S.') + ('%s' % int(trailing))
'''
03/12/2018 -- 21:48:21.432
'''

Python 2 time module: https://docs.python.org/2/library/time.html


Post a Comment for "Can Not Convert 13 Digit Unix Timestamp In Python"