【问题标题】:convert a string into epoch time [duplicate]将字符串转换为纪元时间[重复]
【发布时间】:2018-09-07 19:18:16
【问题描述】:

我尝试将字符串转换为纪元时间但失败了。

from datetime import datetime
from dateutil import parser

dt = '2017-01-01 00:00:00'
dt = parser.parse(dt)
print(dt)

print (datetime(dt).timestamp())

这里是错误:

 print (datetime(dt).timestamp())
   TypeError: an integer is required (got type datetime.datetime)

【问题讨论】:

  • dt 已经是一个日期时间对象,所以你可以在 dt 上调用 timestamp() 方法:dt.timestamp()

标签: python


【解决方案1】:

您只需要使用您的日期时间实例:

print (dt.timestamp())

来自docs

返回与日期时间实例对应的POSIX时间戳。

您当前正在做的是将解析的日期时间对象传递给需要整数的datetime constructor (datetime(dt))。

【讨论】:

    猜你喜欢
    • 2013-01-25
    • 1970-01-01
    • 2016-07-23
    • 2012-04-03
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 2013-12-23
    相关资源
    最近更新 更多