18

我知道如何以相反的方式做到这一点......它将是:

>>> dt.rfc822()
'Sun, 09 Mar 1997 13:45:00 -0500'
4

3 回答 3

34

In [1]: import rfc822     # This only works for python 2 series

In [2]: rfc822.parsedate_tz('Sun, 09 Mar 1997 13:45:00 -0500')
Out[2]: (1997, 3, 9, 13, 45, 0, 0, 1, 0, -18000)

在 python3 中,parsedate_tz 已移至 email.utils


>>> import email.utils   # this works on Python2.5 and up
>>> email.utils.parsedate_tz('Sun, 09 Mar 1997 13:45:00 -0500')
(1997, 3, 9, 13, 45, 0, 0, 1, -1, -18000)
于 2009-10-14T20:43:57.690 回答
7

从 python 3.3 开始,有email.utils.parsedate_to_datetime(date)

>>> from email.utils import parsedate_to_datetime
>>> datestr = 'Sun, 09 Mar 1997 13:45:00 -0500'
>>> parsedate_to_datetime(datestr)
datetime.datetime(1997, 3, 9, 13, 45, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400)))
于 2017-08-08T02:30:56.603 回答
0

如果你去掉时区,你可以这样做:

datetime.datetime.strptime('Sun, 09 Mar 1997 13:45:00', '%a, %d %b %Y %H:%M:%S')
于 2009-10-14T20:48:32.317 回答