How to convert a time string to timestamp by python?

I have a string like “Aug 13 04:21:45 2014“, how can I convert it to timestamp in python way?

With the help of python module time, it’s quite easy. All we need to do is to create a format string to match the given time. There is a list of directives can be embedded in the format string, you can refer time’s library page for details.

>>> import time
>>> string = "Aug 13 04:21:45 2014"
>>> stamp = time.mktime(time.strptime(string, "%b %d %H:%M:%S %Y"))
>>> stamp
1407874905.0

Have fun.

Share Button

Leave a comment

Your email address will not be published. Required fields are marked *