以人类可读的格式转换时间

时间:2019-07-25 11:08:40

标签: python python-3.x epoch

例如,当我给随机的时间作为下一个时间时,我想将时间转换为人类可读的格式

 epoch1 = datetime.datetime.fromtimestamp(1347517370).strftime('%c')
 print(epoch1)

然后一切正常。 但是,如果我使用的是我通过API检索的时间。这不起作用,给我错误

epoch1 = datetime.datetime.fromtimestamp(1563924640001).strftime('%c')
print(epoch1)

这是什么问题?即使我传递变量也不起作用。

epoch1 = datetime.datetime.fromtimestamp(epoch).strftime('%c')

我想从API获取所有时间,并将其转换为人类可读的格式。

高度赞赏的任何帮助

错误:

Traceback (most recent call last):
  File "C:/Users/kiran.tanweer/Documents/Python Scripts/siem/01_GetOffenses.py", line 1042, in <module>
    main()
  File "C:/Users/kiran.tanweer/Documents/Python Scripts/siem/01_GetOffenses.py", line 953, in main
    epoch1 = datetime.datetime.fromtimestamp(1563924640001).strftime('%c')
OSError: [Errno 22] Invalid argument

2 个答案:

答案 0 :(得分:2)

您将从该API获得毫秒级的时间。

除以1000得到fromtimestamp接受的秒数:

>>> datetime.datetime.fromtimestamp(1563924640001 / 1000).strftime('%c')
'Wed Jul 24 02:30:40 2019'

答案 1 :(得分:2)

尝试如下

>>> import datetime
>>> datetime.datetime.fromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')
'2012-09-13 14:22:50' # Local time

在您的情况下,1563924640001156392464000

epoch1 = datetime.datetime.fromtimestamp(156392464000).strftime('%c')
print(epoch1)
# output: Sun Nov 18 04:36:40 6925
相关问题