Morgan Allen Morgan Allen - 1 year ago 126
Python Question

Converting gmail timestamp to strptime

I have this readout

Tue, 19 Jan 2016 16:12:18 -0800 (PST)

I'm storing emails in mongo and I'd like to convert this into a python time object so I can query Mongo based on time of the email.

What is the best way to do this?


Answer Source

One way is to use the strptime functionality of the datetime module,


import datetime
timestamp = "Tue, 19 Jan 2016 16:12:18 -0800 (PST)"
dt = datetime.datetime.strptime(timestamp, "%a, %d %b %Y %H:%M:%S %z (%Z)")

Please read up on Python's datetime module for find the guide about how these format strings work specifically so that you can build your own!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download