python timestamp with timezone code example

Example 1: datetime python

from datetime import datetime as d
date = d.now()
print(date.strftime("%Y-%m-%d %H:%M:%S"))

Example 2: how to convert time from one timezone to another in python

from datetime import datetime
from pytz import timezone
format = "%Y-%m-%d %H:%M:%S %Z%z"
# Current time in UTC
now_utc = datetime.now(timezone('UTC'))
print(now_utc.strftime(format))
# Convert to Asia/Kolkata time zone
now_asia = now_utc.astimezone(timezone('Asia/Kolkata'))
print(now_asia.strftime(format))

Example 3: python datetime from timestamp utc

dt_object = datetime.fromtimestamp(timestamp)

Example 4: how to convert time from one timezone to another in python

$ pip install pytz