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,588
  • 2
  • 14
  • 24
maikelm
  • 393
  • 5
  • 28

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,588
  • 2
  • 14
  • 24
alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148
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
  • 374,366
  • 172
  • 933
  • 1,594