timezone in datetime python code example

Example 1: datetime python timezone

import datetime
import pytz
my_date = datetime.datetime.now(pytz.timezone('US/Pacific'))

Example 2: datetime today

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)

Example 3: python import timezone

from datetime import timezone

Example 4: python set timezone of datetime

datetime_with_timezone = datetime.datetime(2019, 2, 3, 6, 30, 15, 0, pytz.UTC)