我知道如何以相反的方式做到这一点......它将是:
>>> dt.rfc822()
'Sun, 09 Mar 1997 13:45:00 -0500'
答案 0 :(得分:30)
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)
答案 1 :(得分:2)
从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)))
答案 2 :(得分:-1)
如果你剥离时区,你可以这样做:
datetime.datetime.strptime('Sun, 09 Mar 1997 13:45:00', '%a, %d %b %Y %H:%M:%S')