【问题标题】:python datetime fromtimestamp yielding valueerror year out of range [duplicate]python datetime fromtimestamp产生值错误年份超出范围[重复]
【发布时间】:2015-10-11 10:38:01
【问题描述】:

当尝试将浮点格式的时间戳(例如 1437506779950.0)转换为日期时间对象时,我收到一个 ValueError “年份超出范围”。

我使用的这段代码在 3 个月前还有效。现在重新审视它,奇怪的是现在抛出这个错误但代码库中没有任何改变,只有传递给它的数据,唯一改变的数据显然是时间戳。

>>> f = 1437506779950.0
>>> datetime.datetime.fromtimestamp(float(f))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: year is out of range

我不明白要做出什么改变才能打破这种局面?

【问题讨论】:

  • this question。您可能没有从毫秒转换为秒吗?
  • 您确定这是 3 个月前的确切值吗?即便如此 - f = 143750677995.0(你的数字除以 10)让我在 6525 年登陆。
  • samalamma708:你可能想把它作为答案,似乎这就是问题所在。

标签: python datetime


【解决方案1】:

正如this question 的答案中所述,这看起来像是一个单位转换问题。您必须将时间戳除以 1000 才能从毫秒转换为秒。

如果您想保留毫秒精度,请改为除以 1000.0。

【讨论】:

    猜你喜欢
    • 2017-03-28
    • 2016-10-19
    • 2017-09-15
    • 1970-01-01
    • 2021-11-03
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    相关资源
    最近更新 更多