maikelm
maikelm

Reputation: 403

Python convert date string to timestamp

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.

Upvotes: 1

Views: 1922

Answers (2)

alecxe
alecxe

Reputation: 473763

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())

Upvotes: 6

jfs
jfs

Reputation: 414089

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.

Upvotes: 1

Related Questions