Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert date to timestamp in Python

I have a database containing dates formatted like: "2015-10-20 22:24:46". I need to convert these dates to UNIX timestamps (UTC), what's the easiest way to do it with Python?

like image 485
Luis Cruz Avatar asked Nov 10 '15 02:11

Luis Cruz


People also ask

How do I convert a string to a timestamp in Python?

Import the datetime library. Use the datetime. datetime class to handle date and time combinations. Use the strptime method to convert a string datetime to a object datetime.


1 Answers

import time
timestamp = time.mktime(time.strptime('2015-10-20 22:24:46', '%Y-%m-%d %H:%M:%S'))

For more on the format string with all the % symbols, see python's time library.

like image 144
Nick White Avatar answered Oct 24 '22 23:10

Nick White