Python Epoch Timestamp to DateTime

Jul 15, 2019
DateTime to Timestamp, get current Timestamp

Convert timestamp to local datetime

import datetimetimestamp = 1563163320 # in seconds, not milisecondsd = datetime.datetime.fromtimestamp(timestamp)  # to local timeprint(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 timetimestamp_seconds = int(time.time())timestamp_miliseconds = int(time.time()*1000)

Convert DateTime to Timestamp

d = datetime.datetime.now()timestamp = d.timestamp()

Convert DateTime to UTC Timestamp

utc_timestamp = d.replace(tzinfo=datetime.timezone.utc).timestamp()

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.