How to compare time in different time zones in Python?


In this article, we will show you how to compare time to different timezones in Python using the below methods.

  • Comparing the given Timezone with the local TimeZone

  • Comparing the Current Datetime of Two Timezones

  • Comparing Two Times with different Timezone

Method 1: Comparing the given Timezone with the local TimeZone

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Use the import keyword, to import the datetime, pytz modules.

  • Use the timezone() function (gets the time zone of a specific location) of the pytz module, to get the timezone of "CET" (local TimeZone) and store it in a variable.

  • Get the UTC timezone using the timezone() function and store it in another variable.

  • Convert the above time to another time zone using the datetime.astimezone() function (The datetime.astimezone() function is used to modify objects of the DateTime class in the DateTime module) by passing the local time zone as an argument to it.

  • Print the values of local and UTC timeZones.

  • Compare whether the value of the local time zone is equal to the UTC timezone using the if conditional statement and print according to it.

The following program returns the user message after comparing both the local timezone with the given timezone −

Example

# importing datetime, pytz modules from datetime import datetime import pytz # Getting the local timezone localTimeZone = pytz.timezone('CET') # Getting the UTC timeZone utcTimeZone = datetime.now(pytz.utc) # format string format = '%Y:%m:%d %H:%M:%S %Z %z' # Convert the time to the local timezone local = utcTimeZone.astimezone(localTimeZone) # Getting formatted time using strftime() function print("Formatted DateTime in Local Timezone : ",local.strftime(format)) print("Formatted DateTime in UTC Timezone : ",utcTimeZone.strftime(format)) difference = int(local.strftime('%z')) difference2 = int(utcTimeZone.strftime('%z')) # Comparing Time Zones if (difference > difference2): print('UTC TimeZone is behind the Local Timezone by',local.strftime('%z'),'Hours') if(difference < difference2): print('UTC TimeZone is ahead of the Local TimeZone by',utcTimeZone.strftime('%z'),'Hours')

Output

Formatted DateTime in Local Timezone :  2022:09:14 12:05:05 CEST +0200
Formatted DateTime in UTC Timezone :  2022:09:14 10:05:05 UTC +0000
UTC TimeZone is behind the Local Timezone by +0200 Hours

Method 2: Comparing the Current Datetime of Two Timezones

Time zones can make things more complicated, but happily, we can use the same logic for the comparison. The only difference is that we're working with aware dates, which include additional information about the time zone in where they're located.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Use the import keyword, to import the datetime (To work with dates and times, Python has a module called datetime), pytz modules(to make our naive dates aware).

  • Use the timezone() function(gets the time zone of a specific location) of the pytz module, to get the timezone of “America/New_York”.

  • Use the timezone() function(gets the time zone of a specific location) of the pytz module, to get the timezone of “Europe/London”.

  • Give the current date-time using the datetime.now() function.

  • Convert the above timezones to the localize function using the localize() function

localize() function
When creating datetime-aware objects with an initial fixed datetime value, the correct function to use is localise(). The original datetime value will be retained in the resulting datetime aware object.
  • Now both times are at the same timezone

  • Compare the timezones as per the requirements using if conditional statement

Example

# importing datetime, pytz modules from datetime import datetime import pytz # Getting the time in America/New_York timezone timezone_newyork= pytz.timezone('America/New_York') # Getting the time in Europe/London timezone timezone_london = pytz.timezone("Europe/London") # current datetime inputDatetime = datetime.now() # Localize the given date, according to the timezone objects # Here this step converts the given two timezones to local time zones using localize() function datewith_tz_newyork = timezone_newyork.localize(inputDatetime) datewith_tz_london = timezone_london.localize(inputDatetime) # These are now, effectively no longer the same *date* after being localized print("The date and time with timezone newyork:", datewith_tz_newyork) print("The date and time with timezone london:", datewith_tz_london) difference = int(datewith_tz_newyork.strftime('%z')) difference2 = int(datewith_tz_london.strftime('%z')) # checking whether the date with different timezones are equal or NOT after they are in the same timezone if(datewith_tz_newyork > datewith_tz_london): print('Current Time in Newyork time is older than London by',(difference2- difference)/100,'hours') else: print('Current Time in London time is older than Newyork by',(difference- difference2)/100,'hours')

Output

The date and time with timezone newyork: 2022-09-14 10:13:27.727111-04:00
The date and time with timezone london: 2022-09-14 10:13:27.727111+01:00
Current Time in Newyork time is older than London by 5.0 hours

Method 3: Comparing Two Times with different Timezone

It is similar to the previous method, except that we have provided the DateTime of the two timezones.

Example

from datetime import datetime import pytz # Getting the time in America/New_York timezone timezone_newyork= pytz.timezone('America/New_York') # Getting the time in Europe/London timezone timezone_london = pytz.timezone("Europe/London") # input the date time of the newyork in the format Year, Month, Day, Hour, Minute, Second newyorkDateTime = datetime(2013, 3, 15, 20, 5, 10) #input date time of the london londonDateTime = datetime(2013, 3, 15, 20, 5, 10) # Localize the given date, according to the timezone objects datewith_tz_newyork = timezone_newyork.localize(newyorkDateTime) datewith_tz_london = timezone_london.localize(londonDateTime) # These are now, effectively no longer the same *date* after being localized print("The date and time with timezone newyork:", datewith_tz_newyork) print("The date and time with timezone london:", datewith_tz_london) difference = int(datewith_tz_newyork.strftime('%z')) difference2 = int(datewith_tz_london.strftime('%z')) # comparingthe date with different timezonesafter they are in the same timezone if(difference > difference2): print('Given Two Times of two different Time Zones are equal',(difference-difference2)/100,'hours') else: print('Given Two Times of two different Time Zones are not equal by',(difference2-difference)/100,'hours')

Output

The date and time with timezone newyork: 2013-03-15 20:05:10-04:00
The date and time with timezone london: 2013-03-15 20:05:10+00:00
Given Two Times of two different Time Zones are not equal by 4.0 hours

Conclusion

We learned how to compare the time in different timezones using three different methods in this article. We also learned how to compare local time to the specified timezone.

Updated on: 28-Sep-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements