python – 将带有时区的日期字符串转换为epoch

我正在尝试将包含时区的日期字符串转换为纪元时间.

(
python 2.7)

Mon, 08 Jun 2009 19:37:51 GMT

我试着这样做:

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

我得到的错误是:

ValueError: Invalid format string

问题是什么?怎么修好?

谢谢.

最佳答案 使用timestamp()方法的Python 3.5解决方案:

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

产量:

1244489871.0

有建议不使用strftime

参考文献:

> Convert python datetime to epoch with strftime
> https://dateutil.readthedocs.io/en/stable/examples.html#parse-examples

点赞