Reputation: 1800
I am receiving 3 types of date format from the response.
Tue, 24 Dec 2019 05:19:34 +0000 (GMT)
23 Dec 2019 08:00:10 -0500
Mon, 23 Dec 2019 08:00:10 -0500
I have used the below code to convert given date-time into the python DateTime.
date = datetime.datetime.strptime(receive_date, '%a, %d %b %Y %H:%M:%S %z (%Z)')
date = datetime.datetime.strptime(receive_date, '%d %b %Y %H:%M:%S %z')
date = datetime.datetime.strptime(receive_date, '%a, %d %b %Y %H:%M:%S %z')
But I don't find a way to check which format I am receiving from the response.
Upvotes: 1
Views: 129
Reputation: 5854
You can use dateutil to detect the date.
import dateutil.parser
d = dateutil.parser.parse(your_date_str)
Sample output
>>> your_date_str = 'Tue, 24 Dec 2019 05:19:34 +0000 (GMT)'
>>> d = dateutil.parser.parse(your_date_str)
>>> d
datetime.datetime(2019, 12, 24, 5, 19, 34, tzinfo=tzutc())
hope this helps you
Upvotes: 3