如何使用python从纪元转换为日期时间时获得毫秒部分 [英] How to get the millisecond part while converting to date time from epoch using python
本文介绍了如何使用python从纪元转换为日期时间时获得毫秒部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的unip epoc时间为1571205166751
I have a unix epoc time as 1571205166751
我想将其转换为以毫秒为单位的日期时间
I want to convert it to date time in millisecond
期望的结果应该是2019-10-16 05:52:46:751 AM
The desired result should be 2019-10-16 05:52:46:751 AM
但是使用以下代码我得到的结果为2019-10-16 05 :52:46,而不是毫秒部分。
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 ?
PS我想隐蔽时代,而不是其他任何格式。
P.S. I want to covert epoch time, not any other format.
推荐答案
使用 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
这篇关于如何使用python从纪元转换为日期时间时获得毫秒部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文