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

巧了我就是萌 提交于 2019-12-25 01:53:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!