- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to compare time in different time zones in Python?
When you have 2 different time objects with different timezones and you need to compare them, you first need to understand the difference between aware and naive objects. An aware datetime object is an object that holds the information about the timezone while naive object doesn't hold any timezone information.
The easiest way to compare 2 aware objects to check whether they tell the same time or not is to directly compare them.
import datetime, pytz local_tz = pytz.timezone('CET') # Get the time in UTC utc = datetime.datetime.now(pytz.utc) # Convert the time to local timezone local = utc.astimezone(local_tz) print("UTC: ", utc) print("Local: ", local) print(utc == local)
This will give the output −
UTC: 2018-01-03 17:02:43.632805+00:00 Local: 2018-01-03 18:02:43.632805+01:00 True
- How to plot Time Zones in a map in Matplotlib
- Compare time and money.
- Why do we have a different time zones and why GMT is considered as the standard?
- How to convert date and time with different timezones in Python?
- Java Program to get the difference between two time zones by seconds
- How to compare two dates along with time in Java?
- How to combine date and time from different MySQL columns to compare with the entire DateTime?
- How to access and convert time using the time library in Python
- What are various ways to compare time values in Java?
- How to measure elapsed time in python?
- How to compare DateTime Column with only Date not time in MySQL?
- Time Functions in Python?
- Adding Time in Python
- How to calculate the difference between time in different MySQL columns?