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

问题描述 投票:0回答:1

我有一个unipo 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。我想隐蔽时代,而不是其他任何格式。

python datetime timestamp epoch python-datetime
1个回答
1
投票

使用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
© www.soinside.com 2019 - 2024. All rights reserved.