Python – Convert Local Datetime to Different Timezone | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

Example 1

# Python 3.9.5

from datetime import datetime
from pytz import timezone
from tzlocal import get_localzone

# Get Local Time Zone and convert into String
local = get_localzone()
local = str(local)
print(local)

# Convert String into Datetime Object
my_date = "2021-01-01T01:01:01"
my_date = datetime.strptime(my_date, "%Y-%m-%dT%H:%M:%S")
print(my_date)

# Create 2 timezone
old_timezone = timezone(local)
new_timezone = timezone("UTC")

# Convert Local Date with Local Time Zone
localized_timestamp = old_timezone.localize(my_date)
print(localized_timestamp)

# Now Convert Local Date with Diferent Time Zone
new_timezone_timestamp = localized_timestamp.astimezone(new_timezone)
print(new_timezone_timestamp)

Output:

Asia/Calcutta
2021-01-01 01:01:01
2021-01-01 01:01:01+05:30
2020-12-31 19:31:01+00:00

Example 2

# Python 3.9.5

from datetime import datetime
from pytz import timezone

# Get Local Time Zone
print(datetime.now().astimezone().tzinfo)

# Convert String into Datetime Object
local_date = "2021-01-01T01:01:01"
local_date = datetime.strptime(local_date, "%Y-%m-%dT%H:%M:%S")
print(local_date)

# We can also create datetime object passing specific values
#local_date = datetime(local_date.year, local_date.month, local_date.day, local_date.hour, local_date.minute, local_date.second)
#print(local_date)

#local_date = datetime(2021, 1, 1, 1, 1, 1)
#print(local_date)

local_date = local_date.replace(tzinfo = datetime.now().astimezone().tzinfo)
print(local_date)

tz = timezone('UTC')
local_date = local_date.astimezone(tz)
print(local_date)

Output:

India Standard Time
2021-01-01 01:01:01
2021-01-01 01:01:01+05:30
2020-12-31 19:31:01+00:00

Example 3

# Python 3.9.5

from datetime import datetime
import time

ts = time.time()
print(ts)

# Convert String into Datetime Object
local_date = "2021-01-01T01:01:01"
local_date = datetime.strptime(local_date, "%Y-%m-%dT%H:%M:%S")
print(local_date, local_date.timestamp())

# Get Local Date Time
print(local_date.fromtimestamp(local_date.timestamp()))

# Get Date Time using current "ts" (timestamp)
#print(local_date.fromtimestamp(ts))

# Convert Local Date Time to UTC Date Time
print(datetime.utcfromtimestamp(local_date.timestamp()))

# Get UTC Date Time using "ts" (timestamp)
#print(datetime.utcfromtimestamp(ts))

Output:

1622651483.5547616
2021-01-01 01:01:01 1609443061.0
2021-01-01 01:01:01
2020-12-31 19:31:01

Example 4

# Python 3.9.5
# Also works in 2.7.X

from datetime import datetime
import time

# Convert String into Datetime Object
local_date = "2021-01-01T01:01:01"
local_date = datetime.strptime(local_date, "%Y-%m-%dT%H:%M:%S")
print(local_date)

dtt = local_date.timetuple()
print(dtt)

print(time.mktime(dtt))

print(local_date.fromtimestamp(time.mktime(dtt)))

print(datetime.utcfromtimestamp(time.mktime(dtt)))

Output:

2021-01-01 01:01:01
time.struct_time(tm_year=2021, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=1, tm_sec=1, tm_wday=4, tm_yday=1, tm_isdst=-1)
1609443061.0
2021-01-01 01:01:01
2020-12-31 19:31:01

Leave a comment