大纪元时间转换错误年份

时间:2015-02-10 18:59:05

标签: python epoch

我尝试使用Python转换纪元时间。然而,这一年似乎总是错误的。在下面的例子中,它应该是2014年。

import time
timestamp = time.strftime("%a, %d %b %Y %H:%M:%S +0000", 
                    time.localtime(1415219530834))

我做错了什么?

我得到了这个结果:

Sat, 09 Jul 46816 16:20:34 +0000

4 个答案:

答案 0 :(得分:4)

您正在以毫秒为单位传递时间,但它应该以秒为单位。除以1000

import time
timestamp = time.strftime("%a, %d %b %Y %H:%M:%S +0000", 
                    time.localtime(1415219530))

结果:

'Wed, 05 Nov 2014 15:32:10 +0000'

答案 1 :(得分:1)

您已经以毫秒为单位传递了时间戳,但localtime需要一个以秒为单位的值。

time.localtime(1415219530834 / 1000)

答案 2 :(得分:0)

你在Python中以毫秒为单位获得了Epoch。你需要在几秒钟内得到它才能正常工作。

这样的东西
import time
mytime = 1415219530834
seconds = int(mytime/1000)
timestamp = time.strftime("%a, %d %b %Y %H:%M:%S +0000",time.localtime(seconds))

答案 3 :(得分:0)

试试这个

    import datetime
datetime.datetime.fromtimestamp(your time in epoch/1000)

在纪元中转换时间

int(yourtime.strftime("%s"))*1000