将带时区的日期字符串转换为纪元 [英] Converting date string with timezone to epoch

查看:60
本文介绍了将带时区的日期字符串转换为纪元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to convert this date string including timezone to epoch time. (python 2.7)

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

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:无效的格式字符串

ValueError: Invalid format string

出什么问题了?怎么解决?

What is the problem? How can it be fixed?

谢谢.

推荐答案

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

Python 3.5 solution using timestamp() method:

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

产生:

1244489871.0

对于不使用 strftime

参考文献:

  1. 使用strftime将python日期时间转换为纪元
  2. https://dateutil.readthedocs.io/en/stable/examples.html#parse-examples

这篇关于将带时区的日期字符串转换为纪元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆