Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert RFC822 to a python datetime object?

Tags:

python

rfc822

I know how to do this the other way around... it would be:

>>> dt.rfc822()
'Sun, 09 Mar 1997 13:45:00 -0500'
like image 557
Udbhav Avatar asked Oct 14 '09 20:10

Udbhav


People also ask

How do you convert an object to a datetime object in Python?

The date column is indeed a string, which—remember—is denoted as an object type in Python. You can convert it to the datetime type with the . to_datetime() method in pandas .

How do I convert a date string to a datetime object?

To convert string to datetime in Python, use the strptime() method. The strptime() is a built-in function of the Python datetime class used to convert a string representation of the​ date/time to a date object.


1 Answers


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)

in python3 parsedate_tz has moved to 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)
like image 60
John La Rooy Avatar answered Oct 27 '22 16:10

John La Rooy