将带有时区的日期字符串转换为纪元

时间:2017-02-08 10:36:09

标签: python datetime

我正在尝试将此日期字符串(包括时区)转换为纪元时间。 (python 2.7)

  

星期一,2009年6月8日19:37:51 GMT

我试着这样做:

from dateutil import parser
parser.parse("Mon, 08 Jun 2009 19:37:51 GMT").strftime('%s')

我得到的错误是:

  

ValueError:格式字符串无效

有什么问题?如何解决?

感谢。

1 个答案:

答案 0 :(得分:2)

Python 3.5解决方案使用timestamp()方法:

from dateutil import parser
print(parser.parse("Mon, 08 Jun 2009 19:37:51 GMT").timestamp())

产生:

1244489871.0

有建议不使用strftime

参考文献:

  1. Convert python datetime to epoch with strftime
  2. https://dateutil.readthedocs.io/en/stable/examples.html#parse-examples
相关问题