Get Current Time in different Timezone using Python
Timezone is defined as a geographical area or region throughout which standard time is observed. It basically refers to the local time of a region or country. Most of the time zones are offset from Coordinated Universal Time (UTC), the world’s standard for time zone.
In order to get the current time of different time zones, we will be using the
pytz python library.
How to get the current time ?
In order to get the local time, we can use the time module. Some important functions of the time module
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
- localtime() – it helps to get the current local time
- strftime(“%H:%M:%S”, t) – it helps to decide the format of the time to be used to display the time
How to get current time in different zones ?
In order to get the current time of a particular timezone there is a need to use the
pytz Python library. Some of the important commands of pytz library are
- utc – it helps to get the standard UTC time zone
- timezone() – it helps to get the time zone of a particular location
- now() – it helps to get the date, time, utc standard in default format
- astimezone() – it helps to convert the time of a particular time zone into another time zone
We will get the local current time of the region and the standard UTC time
Output: UTC in Default Format : 2020-03-31 07:15:59.640418+00:00 IST in Default Format : 2020-03-31 12:45:59.692642+05:30 Date & Time in UTC : 2020:03:31 07:15:59 UTC+0000 Date & Time in IST : 2020:03:31 12:45:59 IST+0530
Comparing the UTC and IST format of time zones of different regions
Output: UTC Format IST Format 2020-03-31 11:21:13 UTC +0000 2020-03-31 16:51:13 IST +0530 2020-03-31 11:21:13 UTC +0000 2020-03-31 16:51:13 IST +0530 2020-03-31 11:21:13 UTC +0000 2020-03-31 16:51:13 IST +0530 2020-03-31 11:21:13 UTC +0000 2020-03-31 16:51:13 IST +0530 2020-03-31 11:21:13 UTC +0000 2020-03-31 16:51:13 IST +0530
Thus we can conclude that although different regions have different regional time zones but when converted to the UTC time zone all of them gave same value. Thus we can say that
- IST is +0530 hrs ahead of UTC
- EDT is -0400 hrs before of UTC
- SAST is +0200 hrs ahead of UTC
- CDT is -0500 hrs before of UTC
- EEST is +0300 hrs ahead of UTC