1

I need to convert string type Wed, 18 May 2016 11:21:35 GMT to timestamp, in Python. I'm using:

datetime.datetime.strptime(string, format)

But I don't want to specify the format for the date type.

Srini
  • 1,626
  • 2
  • 15
  • 25
maikelm
  • 403
  • 6
  • 30

2 Answers2

6

But I don't want to specify the format for the date type.

Then, let the dateutil parser figure that out:

>>> from dateutil.parser import parse
>>> parse("Wed, 18 May 2016 11:21:35 GMT")
datetime.datetime(2016, 5, 18, 11, 21, 35, tzinfo=tzutc())
Srini
  • 1,626
  • 2
  • 15
  • 25
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
1

To parse rfc 822 time string that is used in email, http, and other internet protocols, you could use email stdlib module:

#!/usr/bin/env python
from email.utils import parsedate_tz, mktime_tz

timestamp = mktime_tz(parsedate_tz("Wed, 18 May 2016 11:21:35 GMT"))

See Converting string to datetime object.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670