如何将字符串中的日期转换为Python中的纪元时间

use*_*980 -2 python

我有一个这样的值:

var="11-feb-15:04:02:21"
Run Code Online (Sandbox Code Playgroud)

我需要转换为纪元时间:

我试过这个:

var.strftime('%s')
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

AttributeError: 'str' object has no attribute 'strftime'
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?我

Jan*_*cke 6

我希望您能仔细阅读这个示例并查找相应的文档。

>>> s = "11-feb-15:04:02:21"
>>> timestruct = time.strptime(s, "%d-%b-%y:%H:%M:%S")
>>> time.mktime(timestruct)
1423623741.0
Run Code Online (Sandbox Code Playgroud)

格式说明符在这里定义:https ://docs.python.org/2/library/time.html#time.strftime

该文档还清楚地表明,当您想将本地时间中的 struct_time转换为自纪元以来的秒数时,您需要使用mktime().