我正在编写一个 python 应用程序,将 YML 文件转换为我博客的静态 HTML 页面(静态站点生成器)。我想添加一个 RSS 提要。在某个地方我读到发布日期必须是:
<pubDate>Wed, 05 Jul 2017 18:38:23 GMT</pubDate>
但是我有:
<pubDate>2017-07-05T18:38:23+00:00</pubDate>
Python 的datetime API 在这些方面确实很麻烦。如何将字符串 2017-07-05T18:38:23+00:00 转换为 GMT?
在此先致谢。
请您参考如下方法:
import datetime
basedate="2017-07-05T18:38:23+00:00"
formatfrom="%Y-%m-%dT%H:%M:%S+00:00"
formatto="%a %d %b %Y, %H:%M:%S GMT"
print datetime.datetime.strptime(basedate,formatfrom).strftime(formatto)
将为您返回正确的转录:2017 年 7 月 5 日星期三,格林威治标准时间 18:38:23
我的格式来源:http://strftime.org/
