Morgan Allen Morgan Allen - 11 days ago 8
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?

Thanks!

Answer

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

i.e.

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!

Comments