我如何将这个时间戳值转换为代表python中的秒数的整数

 2016-08-06T06:07:36.349Z

这是我在 flex 搜索查询中收到的时间戳值。

我尝试搜索,但时间戳格式与此不同,而this却无济于事

最佳答案

您可以使用python内置的datetime包及其strptime方法将字符串转换为datetime对象。

from datetime import datetime
datetime.strptime("2016-08-06T06:07:36.349Z","%Y-%m-%dT%H:%M:%S.%fZ")

之后,您应该获得可以获取的纪元datetime对象
epoch = datetime.utcfromtimestamp(0)

您的最后几秒钟可以从此方法得出
def unix_time_millis(datetime):
    return (datetime - epoch).total_seconds() * 1000.0

所以你完整的代码看起来像
from datetime import datetime

epoch = datetime.utcfromtimestamp(0)

def unix_time_millis(datetime):
    return (datetime - epoch).total_seconds() * 1000.0

current_date = datetime.strptime("2016-08-06T06:07:36.349Z","%Y-%m-%dT%H:%M:%S.%fZ")
print unix_time_millis(current_date)

这个答案的灵感来自这个答案https://stackoverflow.com/a/11111177/4453633

关于python - 将 Elasticsearch 时间戳属性转换为秒,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38818126/

10-09 08:22
查看更多