如何在使用 python 从纪元转换为日期时间时获取毫秒部分

How to get the millisecond part while converting to date time from epoch using python

我的 unix epoc 时间是 1571205166751

我想将它转换为以毫秒为单位的日期时间

期望的结果应该是 2019-10-16 05:52:46:751 AM

但是使用以下代码我得到的结果是 2019-10-16 05:52:46,而不是毫秒部分。

import time
time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(1571205166))

如何在转换时获取毫秒部分?

P.S。我想隐藏纪元时间,而不是任何其他格式。

使用datetime.datetime.fromtimestamp

epoch_time = 1571205166751

dt = datetime.datetime.fromtimestamp(epoch_time/1000)

dt.strftime("%Y-%m-%d %H:%M:%S.%f %p")

输出:

'2019-10-16 11:22:46.751000 AM'

Note:

%f is not supported in time.strftime