Country Date and Time using Python

856 views

Hope you are doing great!

Today, We are going to 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.

pytz brings the Olson tz database into Python. This library allows accurate and cross platform timezone calculations.

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)
Code language: JavaScript (javascript)

Output

['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers',........ 'W-SU', 'WET', 'Zulu']
Code language: JSON / JSON with Comments (json)

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"))
Code language: JavaScript (javascript)

Output

Date is 23-09-20 and time is 00:39:26
Code language: CSS (css)

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

Code With Comments

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')}")
Code language: Python (python)

Output

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

Check out more python projects

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Enable Notifications    OK No thanks