Get Any Country Date And Time Using Python

Get Any Country Date And Time Using Python

Here, in this post, we will see how can we Get Any Country Date And Time Using Python which will be something like a World clock using Python. For this, we need the datetime module and python’s timezone module i.e. pytz.

Before we proceed further, let’s see the video for more detailed and easy explanation–

To install pytz python, type the below command in your terminal–

pip install pytz

We can print the names of all countries whose timezone is available using this module using the following code–

Example

import pytz
time_zones = pytz.all_timezones
print(time_zones)

Output

['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers',........ 'W-SU', 'WET', 'Zulu']

To print the date and time of any country, we need to use both modules i.e. datetime and pytz.

Here is the code to print the date and time of any country–

Example

from datetime import datetime
import pytz
country_time_zone = pytz.timezone('America/New_York')
country_time = datetime.now(country_time_zone)
print(country_time.strftime("Date is %d-%m-%y and time is %H:%M:%S"))

Output

Date is 23-09-20 and time is 00:39:26

In the similar way, we can print the date and time of any number of countries. To do this, we can create the list of desired countries for which we want to get current date and time and can use for loop to print all date and time of all the desired countries.

Now, let’s see the code to Get Any Country Date And Time Using Python–

from datetime import datetime
import pytz
# list of desired countries
Country_Zones = ['America/New_York', 'Asia/Kolkata', 'Australia/Sydney',
                'Canada/Atlantic', 'Brazil/East','Chile/EasterIsland', 'Cuba', 'Egypt',
                'Europe/Amsterdam', 'Europe/Athens', 'Europe/Berlin', 'Europe/Istanbul',
                'Europe/Jersey', 'Europe/London', 'Europe/Moscow', 'Europe/Paris', 
                'Europe/Rome', 'Hongkong', 'Iceland', 'Indian/Maldives', 'Iran',
                'Israel', 'Japan', 'NZ', 'US/Alaska', 'US/Arizona', 'US/Central', 
                'US/East-Indiana']
country_time_zones = []
for country_time_zone in Country_Zones:
    country_time_zones.append(pytz.timezone(country_time_zone))
for i in range(len(country_time_zones)):
    country_time = datetime.now(country_time_zones[i])
    print(f"The date of {Country_Zones[i]} is {country_time.strftime('%d-%m-%y')} and The time of {Country_Zones[i]} is {country_time.strftime('%H:%M:%S')}")

Output

The date of America/New_York is 23-09-20 and The time of America/New_York is 00:46:13
The date of Asia/Kolkata is 23-09-20 and The time of Asia/Kolkata is 10:16:13
The date of Australia/Sydney is 23-09-20 and The time of Australia/Sydney is 14:46:13
The date of Canada/Atlantic is 23-09-20 and The time of Canada/Atlantic is 01:46:13
The date of Brazil/East is 23-09-20 and The time of Brazil/East is 01:46:13
The date of Chile/EasterIsland is 22-09-20 and The time of Chile/EasterIsland is 23:46:13
The date of Cuba is 23-09-20 and The time of Cuba is 00:46:13
The date of Egypt is 23-09-20 and The time of Egypt is 06:46:13
The date of Europe/Amsterdam is 23-09-20 and The time of Europe/Amsterdam is 06:46:13
The date of Europe/Athens is 23-09-20 and The time of Europe/Athens is 07:46:13
The date of Europe/Berlin is 23-09-20 and The time of Europe/Berlin is 06:46:13
The date of Europe/Istanbul is 23-09-20 and The time of Europe/Istanbul is 07:46:13
The date of Europe/Jersey is 23-09-20 and The time of Europe/Jersey is 05:46:13
The date of Europe/London is 23-09-20 and The time of Europe/London is 05:46:13
The date of Europe/Moscow is 23-09-20 and The time of Europe/Moscow is 07:46:13
The date of Europe/Paris is 23-09-20 and The time of Europe/Paris is 06:46:13
The date of Europe/Rome is 23-09-20 and The time of Europe/Rome is 06:46:13
The date of Hongkong is 23-09-20 and The time of Hongkong is 12:46:13
The date of Iceland is 23-09-20 and The time of Iceland is 04:46:13
The date of Indian/Maldives is 23-09-20 and The time of Indian/Maldives is 09:46:13
The date of Iran is 23-09-20 and The time of Iran is 08:16:13
The date of Israel is 23-09-20 and The time of Israel is 07:46:13
The date of Japan is 23-09-20 and The time of Japan is 13:46:13
The date of NZ is 23-09-20 and The time of NZ is 16:46:13
The date of US/Alaska is 22-09-20 and The time of US/Alaska is 20:46:13
The date of US/Arizona is 22-09-20 and The time of US/Arizona is 21:46:13
The date of US/Central is 22-09-20 and The time of US/Central is 23:46:13
The date of US/East-Indiana is 23-09-20 and The time of US/East-Indiana is 00:46:13

Also read:

Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you