【问题标题】:How to conver datetime to timestamp epoch [duplicate]如何将日期时间转换为时间戳 [重复]
【发布时间】:2016-09-14 19:38:58
【问题描述】:

我是 python 2.7 的新手,想知道如何将日期时间转换为时间戳纪元并面临问题。

日期时间也是当前的 UTC。 例子 : 2016-05-11 18:33:44转1462958071

【问题讨论】:

  • @EdChum 是的,这样标记。
  • 问题中的示例不正确:str(datetime.utcfromtimestamp(1462958071)) == '2016-05-11 09:14:31',而不是 2016-05-11 18:33:44,即 utc 时间 (2016-05-11 18:33:44) 不对应于“时间戳纪元”(1462958071)。对应的 POSIX 时间戳为1462991624

标签: python python-2.7 timestamp


【解决方案1】:

将字符串转换为python日期时间对象,然后解析秒

a ="2016-05-11 18:33:44"
b= datetime.strptime(a,"%Y-%d-%m %H:%M:%S")
print b.strftime('%s') # returns time in epoch

【讨论】:

猜你喜欢
  • 2014-08-06
  • 2014-04-11
  • 2018-09-12
  • 2014-06-26
  • 1970-01-01
  • 1970-01-01
  • 2014-09-10
  • 1970-01-01
相关资源
最近更新 更多