在 python 中将字符串日期时间格式转换为以秒为单位的整数时间

Convert string datetime format into integer time in seconds in python

我的输入是input_time = "May 5 2016 11:29:32"。 预期输出应以秒或毫秒为单位,这是整数类型,即 output_time = 2424241313113。 上面的转换应该在python中完成。如何进行转换?

以下是将日期时间转换为纪元秒的方法(日期从 1970 年 1 月 1 日 00:00:00 UTC 开始)

在Python3.3+

from datetime import datetime
datetime.strptime('May 5 2016 11:29:32','%b %d %Y %H:%M:%S').timestamp()

在 Python 2.7.9

datetime.strptime('May 5 2016 11:29:32','%b %d %Y %H:%M:%S').strftime('%s')

请注意strftime('%s')使用您当地的时区。