Python 3 Script to Get Current Time & Timezone of Any City Name Using Geopy & TimezoneFinder Library
60 views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/python-3-script-to-get-current-time-timezone-of-any-city-name-using-geopy-timezonefinder-library/
View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
so in this video I will show you a
0:03
Python script which will
0:06
actually find out the current time and
0:09
time zone of any city in the world you
0:13
just need to basically provide the city
0:15
name and then it will tell you the exact
0:17
time and the time zone information about
0:20
that specific city so I basically built
0:23
out this application you will see
0:27
uh let me run this python script in the
0:30
terminal python app py so here it will
0:34
ask you for the location name as soon as
0:37
you run this application it is saying
0:39
that enter the city name so here you
0:42
will provide your city name let's
0:44
suppose I provide any
0:46
city let's suppose I say
0:49
Pune which is located in Maharashtra you
0:52
will see it will give you this
0:53
information
0:54
such as the latitude
0:57
longitude time zone of Pune you will see
1:01
Asia/colkata which is in India and then
1:03
current time in Pune you'll see the
1:06
actual date and current time which is
1:10
15:452 the date here 15 May
1:13
2025 and then you can repeat this for
1:16
any city let's suppose I say here I want
1:19
to find out time here Sydney which is in
1:21
Australia so if I plug this city name
1:25
enter it you will find out the latitude
1:28
longitude time zone which is Australia/
1:31
Sydney and you will see the time here
1:33
which is
1:35
2016 24 this is the actual time here
1:38
current time in 24hour
1:41
format so you can repeat this for any
1:43
city here it will find out all this
1:46
useful information let's suppose I say
1:49
here I say
1:51
Texas you'll see that America you can
1:55
see current
1:57
time so for this application we are
2:00
using quite a number of packages free
2:02
open-source packages of Python so let me
2:05
show you one by one all the source code
2:07
I have given in the description of the
2:09
video so first of all you need to
2:10
install this package which
2:13
is pyz this is actually a free
2:16
open-source module which returns useful
2:18
information about time zone and of
2:21
certain
2:23
different country names by providing
2:26
city name so you just need to install
2:28
this module this next module you need is
2:32
the module which lets you find out the
2:35
time zone so this is time zone finder so
2:39
the command is simple you simply install
2:41
this package as well for finding out the
2:44
time zones of different locations on the
2:47
earth it will suggest you the
2:49
coordinates such as the latitude
2:51
longitude the third package is for doing
2:53
the
2:54
geocoding it is called as geopy it's a
2:58
Python open-source geocoding package
3:01
which will return the latitude and
3:03
longitude from city name so you also
3:06
need this module as well so these three
3:08
modules are required after installing it
3:11
just create a simple app py file and I
3:13
will show you step by step let me just
3:17
uh write this and just step by step show
3:20
you so just create a simple app py file
3:24
and first of all you need to import all
3:28
these packages one by one geocoders we
3:31
need to import
3:33
this and then from this time zone finder
3:37
we need to import
3:39
this time zone finder class and then
3:43
from this date time we need to import
3:46
date time and then we need to import
3:49
lastly this package
3:51
pytz for time zone information so right
3:55
here
3:56
uh here we just need to specify the city
4:00
name we just create take this from the
4:03
user so here we'll be asking the simple
4:06
question enter your city name then we
4:08
will be defining this
4:11
simple method get time in city and we
4:14
pass the city name as an argument and
4:17
then we will define this function here
4:19
which will be responsible for fetching
4:21
all this information get time in
4:24
city and we passing the city name as an
4:30
argument and right inside this function
4:32
we just need to first of all do the
4:36
geoloccating by using this class
4:40
nomin passing the user agent you can
4:43
just set any name for your user agent
4:45
i'm setting
4:47
this after that we just need
4:50
to find out the location coordinates so
4:53
for doing this it contains this method
4:56
geo code and here you will passing your
4:59
city name and then it will just convert
5:03
this into latitude and
5:06
longitude as soon
5:08
as if I just execute this now
5:14
application and here you just provide
5:17
any city name let's suppose I provide
5:19
Oakuckland you will see it will return
5:21
this overall information the overall
5:26
address and from this we just need
5:30
to convert this into latitude and
5:33
longitude for doing this we first of all
5:35
say if the Location is there in that
5:38
case latitude
5:41
longitude location
5:48
dot and location
5:50
dot
5:53
longitude and then we'll be printing
5:56
these coordinates very
5:57
simply which are contained inside this
6:02
variables like this
6:09
you'll see that it will display the
6:11
latitude and longitude so once you got
6:13
this information once you get these
6:15
coordinates then it becomes really easy
6:18
to locate
6:20
the place so after that we can find out
6:24
the time zone information by calling
6:27
this time zone finder class and then we
6:31
create the variables time zone string
6:35
and using
6:36
this object it contains a function here
6:39
time zone at and here you specify your
6:45
longitude latitude and longitude
6:54
like
6:55
this so then it returns all this time
6:58
zone
7:01
information so you'll be comparing that
7:04
if the time zone is
7:06
available then in that case we just need
7:09
to print
7:10
this
7:17
information so if you just execute this
7:20
application
7:22
now just put a comma right
7:33
here so it will display this time zone
7:36
information as well pacific/ appland you
7:39
would see that after that we can get the
7:41
current time in that particular
7:45
location so here we'll be using this fun
7:48
module pytz it contains this function
7:51
time zone and here we'll be passing the
7:53
time zone string and then we will
7:56
extract the time of the city so using
7:59
the datetime module
8:02
dot passing the time zone and get to get
8:06
the current time so you can see how
8:08
systematically each information is
8:10
extracted from so every other
8:13
information is dependent upon the
8:14
previous information so we are
8:17
extracting the current time from the
8:18
time zone and the time zone is extracted
8:21
from the latitude longitude and the
8:24
latitude longitude was found out by
8:26
using this uh geocoding so all these
8:29
things are interrelated to each other so
8:32
the information is systematically found
8:34
out uh you'll see that if I again
8:38
type any other city
8:42
here you will see that so in this easy
8:45
way you can do this for any city name
8:48
you can build out a complete web
8:50
application as
8:52
well so can
8:59
see so you can see that in this easy way
9:03
you can do this process uh thank you
9:05
very much for watching this video please
9:07
hit that like button subscribe the
9:09
channel and also check out my website
9:12
freemediatools.com
9:14
uh which contains thousands of tools