How to parse a RFC 2822 date/time into a Python datetime?

millenomi picture millenomi · May 19, 2009 · Viewed 12.1k times · Source

I have a date of the form specified by RFC 2822 -- say Fri, 15 May 2009 17:58:28 +0000, as a string. Is there a quick and/or standard way to get it as a datetime object in Python 2.5? I tried to produce a strptime format string, but the +0000 timezone specifier confuses the parser.

Answer

Matt Jones picture Matt Jones · Aug 11, 2009

The problem is that parsedate will ignore the offset.

Do this instead:

from email.utils import parsedate_tz
print parsedate_tz('Fri, 15 May 2009 17:58:28 +0700')