Home Python Project Country Date and Time using Python

Country Date and Time using Python

by anupmaurya

In this article you’ll learn 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.

What is Pytz?

Pytz is a Python library that allows you to work with time zones in a Python application. It provides functionality for working with time zones and performing timezone conversions. The name “pytz” is an abbreviation of “Python Time Zone.” Pytz brings the Olson tz database into Python.

How to install Pytz ?

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 a 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 the current date and time and can use for loop to print all dates and times of all the desired countries.

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

Code With Comments of 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-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

Explanation of Country Date and Time using Python

Let’s break it down step by step:

1. Importing Modules:

  • datetime from datetime module: This provides functions for working with dates and times.
  • pytz module: This library allows you to work with different time zones.

2. Defining Time Zone List:

  • Country_Zones: This list stores the string identifiers for various time zones around the world. Each string represents a specific location (e.g., ‘America/New_York’ for New York time).

3. Creating a List of Time Zone Objects:

  • An empty list country_time_zones is created.
  • The code iterates through the Country_Zones list.
  • For each time zone string, it uses pytz.timezone(country_time_zone) to create a time zone object and append it to the country_time_zones list.

4. Looping through Time Zones and Printing Date & Time:

  • The code loops through the country_time_zones list using a for loop.
  • Inside the loop:
    • It gets the current date and time for the specific time zone using datetime.now(country_time_zones[i]). This creates a datetime object representing the current time in that time zone.
    • It uses strftime methods to format the date and time:
      • %d-%m-%y: This formats the date as day-month-year (e.g., 03-04-24).
      • %H:%M:%S: This formats the time as hours (24-hour format):minutes:seconds (e.g., 12:37:22).
    • Finally, it prints a formatted string that includes the time zone name, date, and time.

Overall, this code retrieves the current date and time for various time zones specified in the Country_Zones list and displays them in a user-friendly format.

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.