Get Current Time in different Timezone using Python
Last Updated :
29 Dec, 2020
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
- 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
Examples:
Python3 1==
import time
curr_time = time.localtime()
curr_clock = time.strftime("%H:%M:%S", curr_time)
print(curr_clock)
Output:
11:58:19
We will get the local current time of the region and the standard UTC time
Examples:
Python3 1==
from datetime import datetime
import pytz
# get the standard UTC time
UTC = pytz.utc
# it will get the time zone
# of the specified location
IST = pytz.timezone('Asia/Kolkata')
# print the date and time in
# standard format
print("UTC in Default Format : ",
datetime.now(UTC))
print("IST in Default Format : ",
datetime.now(IST))
# print the date and time in
# specified format
datetime_utc = datetime.now(UTC)
print("Date & Time in UTC : ",
datetime_utc.strftime('%Y:%m:%d %H:%M:%S %Z %z'))
datetime_ist = datetime.now(IST)
print("Date & Time in IST : ",
datetime_ist.strftime('%Y:%m:%d %H:%M:%S %Z %z'))
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
Examples:
Python3 1==
from datetime import datetime
import pytz
UTC = pytz.utc
timeZ_Kl = pytz.timezone('Asia/Kolkata')
timeZ_Ny = pytz.timezone('America/New_York')
timeZ_Ma = pytz.timezone('Africa/Maseru')
timeZ_Ce = pytz.timezone('US/Central')
timeZ_At = pytz.timezone('Europe/Athens')
dt_Kl = datetime.now(timeZ_Kl)
dt_Ny = datetime.now(timeZ_Ny)
dt_Ma = datetime.now(timeZ_Ma)
dt_Ce = datetime.now(timeZ_Ce)
dt_At = datetime.now(timeZ_At)
utc_Kl = dt_Kl.astimezone(UTC)
utc_Ny = dt_Ny.astimezone(UTC)
utc_Ma = dt_Ma.astimezone(UTC)
utc_Ce = dt_Ce.astimezone(UTC)
utc_At = dt_At.astimezone(UTC)
print("UTC Format \t\t\t IST Format")
print(utc_Kl.strftime('%Y-%m-%d %H:%M:%S %Z %z'),
"\t ",
dt_Kl.strftime('%Y-%m-%d %H:%M:%S %Z %z'))
print(utc_Ny.strftime('%Y-%m-%d %H:%M:%S %Z %z'),
"\t ",
dt_Kl.strftime('%Y-%m-%d %H:%M:%S %Z %z'))
print(utc_Ma.strftime('%Y-%m-%d %H:%M:%S %Z %z'),
"\t ",
dt_Kl.strftime('%Y-%m-%d %H:%M:%S %Z %z'))
print(utc_Ce.strftime('%Y-%m-%d %H:%M:%S %Z %z'),
"\t ",
dt_Kl.strftime('%Y-%m-%d %H:%M:%S %Z %z'))
print(utc_At.strftime('%Y-%m-%d %H:%M:%S %Z %z'),
"\t ",
dt_Kl.strftime('%Y-%m-%d %H:%M:%S %Z %z'))
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
Similar Reads
How to Get Current Date and Time using Python In this article, we will cover different methods for getting data and time using the DateTime module and time module in Python.Different ways to get Current Date and Time using PythonCurrent time using DateTime objectGet time using the time moduleGet Current Date and Time Using the Datetime ModuleIn
6 min read
Get Current Timestamp Using Python A timestamp is a sequence of characters that represents the date and time at which a particular event occurred, often accurate to fractions of a second. Timestamps are essential in logging events, tracking files, and handling date-time data in various applications. There are 3 different ways to get
2 min read
How to convert date and time with different timezones in Python? In this article, we will discuss how to convert date and time with different timezones in Python. To do this we will use one of the modules of python pytz . This module brings the Olson tz database into Python and this library allows accurate and cross-platform timezone calculations using Python. Th
2 min read
Get current date using Python Prerequisite: DateTime moduleIn Python, Date and Time are not data types of their own, but a module named DateTime can be imported to work with the date as well as time. Datetime module comes built into Python, so there is no need to install it externally. The DateTime module provides some functions
3 min read
Get current time in milliseconds using Python In this article, we will get the time in milliseconds using Python. Here, we are using two inbuild modules of Python that is Datetime module and the Time Module to get the time in milliseconds. Get time in milliseconds using the DateTime module To get the time in milliseconds we used the DateTime mo
1 min read
Python time.timezone Constant Python time. timezone constant returns the local (non-DST) timezone offset in UTC format, where DST stands for Daylight Saving Time. It is negative in most of Western Europe, positive in the US, zero in the UK). Â This constant returns the current timezone in which you reside. Python3 import time pri
1 min read
How to Format date using strftime() in Python ? In this article, we will see how to format date using strftime() in Python. localtime() and gmtime() returns a tuple representing a time and this tuple is converted into a string as specified by the format argument using python time method strftime(). Syntax: time.strftime(format[, sec]) sec: This i
2 min read
Convert string to datetime in Python with timezone Converting a string to a datetime in Python with timezone means parsing a date-time string and creating a timezone-aware datetime object. For example, a string like '2021-09-01 15:27:05.004573 +0530' can be converted to a Python datetime object that accurately represents the date, time and timezone.
2 min read
Handling timezone in Python There are some standard libraries we can use for timezones, here we'll use pytz. This library has a timezone class for handling arbitrary fixed offsets from UTC and timezones. Installation pytz is a third-party package that you have to install. To install pytz use the following command - pip install
4 min read
How to add hours to the current time in Python? Prerequisites: Datetime module Every minute should be enjoyed and savored. Time is measured by the hours, days, years, and so on. Time helps us to make a good habit of organizing and structuring our daily activities. In this article, we will see how we can extract real-time from a python module. The
3 min read