Python Epoch Timestamp to DateTime

July 15, 2019
DateTime to Timestamp, get current Timestamp

Convert timestamp to local datetime

import datetime

timestamp = 1563163320 # in seconds, not miliseconds

d = datetime.datetime.fromtimestamp(timestamp)  # to local time
print(d.strftime('%Y-%m-%d %H:%M:%S'))          # '2019-07-15 12:02:00'

Convert timestamp to UTC datetime

d = datetime.datetime.utcfromtimestamp(timestamp)

Get current timestamp

import time

timestamp_seconds = int(time.time())
timestamp_miliseconds = int(time.time()*1000)

Convert DateTime to Timestamp

d =
timestamp = d.timestamp()

Convert DateTime to UTC Timestamp

utc_timestamp = d.replace(tzinfo=datetime.timezone.utc).timestamp()
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.