【问题标题】:Python: What is the easiest way to convert from time.time() value to time.strftime() value?Python:从 time.time() 值转换为 time.strftime() 值的最简单方法是什么?
【发布时间】:2012-03-22 20:48:53
【问题描述】:

我需要以time.time() 形式获得的浮点值以("%m/%d/%Y %H:%M:%S") 格式表示时间。

我已经有一个 time.time() 形式的值。例如,我已经有了每 0.3 秒从设备获取的值,例如 1332449493.0,......,然后我想将 1332449493.0 转换为 strftime 格式。

前: time.strftime("%m/%d/%Y %H:%M:%S", 1332449493.0) 将抛出 TypeError 因为 strf 需要 9 项元组而不是浮点数。

【问题讨论】:

  • 通过...使用strftime 函数。你读过文档吗?
  • 是的,但是 time.strftime("%m/%d/%Y %H:%M:%S", time.localtime(1332449493.0)) 是我自 time.strftime 以来一直在寻找的("%m/%d/%Y %H:%M:%S", 1332449493.0) 不是正确的方法。

标签: python


【解决方案1】:
time.strftime("%m/%d/%Y %H:%M:%S", time.localtime(1332449493.0))

完成这项工作。

【讨论】:

  • 非常感谢!正是我想要的:)
【解决方案2】:

您可以轻松地从中获得datetime

datetime.datetime.fromtimestamp(time.time())

然后你可以随意格式化它。

【讨论】:

  • 我想我没有把我的问题说得很清楚。所以我已经有了一个 time.time() 形式的值。例如,我已经有一个值,我每 0.3 秒从一个设备获得一个值,比如 1332449493.0,......,然后我想将 1332449493.0 转换为 strftime 格式。你会怎么做?我不是在问如何获得时间戳
  • @Pythomania: fromtimestamp 这个名字用词不当,它可以处理time
猜你喜欢
  • 2015-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-09
  • 1970-01-01
  • 1970-01-01
  • 2012-02-05
相关资源
最近更新 更多