Ketan Modi
Ketan Modi

Reputation: 1800

check datetime has day name or timezone using python

I am receiving 3 types of date format from the response.

  1. Tue, 24 Dec 2019 05:19:34 +0000 (GMT)
  2. 23 Dec 2019 08:00:10 -0500
  3. Mon, 23 Dec 2019 08:00:10 -0500

I have used the below code to convert given date-time into the python DateTime.

  1. date = datetime.datetime.strptime(receive_date, '%a, %d %b %Y %H:%M:%S %z (%Z)')
  2. date = datetime.datetime.strptime(receive_date, '%d %b %Y %H:%M:%S %z')
  3. 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

Answers (1)

rahul.m
rahul.m

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

Related Questions