问题
I have a unix epoc time as 1571205166751
I want to convert it to date time in millisecond
The desired result should be 2019-10-16 05:52:46:751 AM
But using following code I am getting the result as 2019-10-16 05:52:46, not the millisecond part.
import time
time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(1571205166))
How to get the millisecond part while converting ?
P.S. I want to covert epoch time, not any other format.
回答1:
Use datetime.datetime.fromtimestamp
epoch_time = 1571205166751
dt = datetime.datetime.fromtimestamp(epoch_time/1000)
dt.strftime("%Y-%m-%d %H:%M:%S.%f %p")
output:
'2019-10-16 11:22:46.751000 AM'
Note:
%f is not supported in time.strftime
来源:https://stackoverflow.com/questions/58410614/how-to-get-the-millisecond-part-while-converting-to-date-time-from-epoch-using-p