convert datetime to unix timestamp python code example
Example 1: unix to date python
>>> from datetime import datetime
>>> ts = int("1284101485")
>>> print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))
... '2010-09-10 06:51:25'
Example 2: how to get unix timestamp in python
import time
time.time()
Example 3: convert integer unix to timestamp python
import datetime
print(
datetime.datetime.fromtimestamp(
int("1284105682")
).strftime('%Y-%m-%d %H:%M:%S')
)
Example 4: get datetime from unix timestamp python3
from datetime import datetime
timestamp = 1545730073
dt_object = datetime.fromtimestamp(timestamp)
print("dt_object =", dt_object)
print("type(dt_object) =", type(dt_object))
Example 5: pandas datetime to unix timestamp
dates = pd.to_datetime(['2019-01-15 13:30:00'])
(dates - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s')