- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to display date in different country format
In Python, we have some time in-built functions like datetime.now(), utcnow(), astimezone(), and strftime() that can be used to display time in the different country formats. In this program, we will use the modules namely datetime(to access all content related to date and time) and pytz(allows for timezone calculation and conversion of datetime). To get the timezone of different countries the UTC will be set by an in-built function named utcnow(). The problem statement follows some built-in functions of Python such as datetime.now(), utcnow(), astimezone(), and strftime() that will be used to display time in different country formats.
Syntax
The following syntax is used in the examples −
datetime.now()
This is an inbuilt function of time in Python to find the current date and time.
utcnow()
This is an in-built function that returns the time in UTC timezone.
astimezone()
This is an in-built function that returns the datetime and specified timezone parameter.
strftime()
The strftime() function is an in-built function that accepts the parameter to declare the format of time.
now()
This is an in-built function in Python that can be primarily used to find the current time and date.
date.to('Country_time_zone').format('YYYY-MM-DD HH:mm:ss'))
The above syntax is used in example 3 −
date.to(‘County_time_zone’): The date is the name of the variable that sets the values as utcnow() with the arrow module. The Country_time_zone is set as the zone of the country. For example- India- Asia/Kolkata, China- Asia/Shanghai, etc.
format('YYYY-MM-DD HH:mm:ss'): The format is primarily used to set the data and time format of the country.
Example 1
In the following example, will start the program by importing the modules namely datetime and pytz. Then we create the variable get_utc to store the value of datetime using an in-built function named datetime.utcnow() that will provide the universal standard time. Then create the variables for different countries(For eg. get_countries_name) and set the values by using an in-built function namely- astimezone() and timezone() with the help of the module named pytz. Next, store the format of the time in the variable format_str. This time format pass as a parameter in the strftime() function to format the time of different countries. Then create the time variable of different countries and set the value by the format of time using the strftime() function. Finally, we are printing the variable with the help of different time variables of countries.
import datetime import pytz # Using the in-built function datetime.now() get_utc = datetime.datetime.utcnow() # Timezone of different countries get_russia = get_utc.astimezone( pytz.timezone( 'Europe/Moscow' ) ) get_india = get_utc.astimezone( pytz.timezone( 'Asia/Kolkata' ) ) get_china = get_utc.astimezone( pytz.timezone( 'Asia/Shanghai' ) ) get_srilanka = get_utc.astimezone( pytz.timezone( 'Asia/Colombo' ) ) # We will store the format style and passes to the strftime() function format_str = '%Y-%m-%d %H:%M:%S %Z%z' time_russia = get_russia.strftime( format_str ) time_india = get_india.strftime( format_str ) time_china = get_china.strftime( format_str ) time_srilanka = get_srilanka.strftime( format_str ) # display the local time of different countries print( "**************The Time format of different countries**************" ) print( f"Russia: { time_russia }" ) print( f"India: { time_india }" ) print( f"China: { time_china }" ) print( f"Sri Lanka: { time_srilanka }" )
Output
**************The Time format of different countries************** Russia: 2023-05-29 05:17:14 MSK+0300 India: 2023-05-29 07:47:14 IST+0530 China: 2023-05-29 10:17:14 CST+0800 Sri Lanka: 2023-05-29 07:47:14 +0530+0530
Example 2
Installation Requirement
pip install pendulum
Install this command to your system to run the program based on the pendulum module.
Install this command to your system to run the program based on the pendulum module.
In the following example, we will start the program by importing the module named pendulum. Then use the built-in method now() that will be used to find the current time and date with the pendulum variable in the variable date. Next, we are using the multiple print function that sets the different time zones of countries as a result.
import pendulum date = pendulum.now() print("The date and time of different countries:\n") print("New York: ", date.in_timezone('America/New_York').to_datetime_string()) print("London: ", date.in_timezone('Europe/London').to_datetime_string()) print("Paris: ", date.in_timezone('Europe/Paris').to_datetime_string()) print("India: ", date.in_timezone('Asia/Kolkata').to_datetime_string())
Output
The date and time of different countries: New York: 2023-05-29 04:06:37 London: 2023-05-29 09:06:37 Paris: 2023-05-29 10:06:37 India: 2023-05-29 13:36:37
Example 3
Installation Requirement
pip install arrow
Install this command to your system to run the program based on the arrow module.
In the following example, begin the program by importing the module named arrow. Then use the built-in method utcnow() with arrow module that will find date and time of different counties and store it in the variable date. Next, set the date using to() with a variable date which will find the dates of all different countries and format to set the time and get the result using the print() function.
import arrow date = arrow.utcnow() print("*****Different Timezones*****") print("New York: ", date.to('America/New_York').format('YYYY-MM-DD HH:mm:ss')) print("India: ", date.to('Asia/Kolkata').format('YYYY-MM-DD HH:mm:ss')) print("London: ", date.to('Europe/London').format('YYYY-MM-DD HH:mm:ss')) print("Paris: ", date.to('Europe/Paris').format('YYYY-MM-DD HH:mm:ss'))
Output
*****Different Timezones***** New York: 2023-05-29 04:15:08 India: 2023-05-29 13:45:08 London: 2023-05-29 09:15:08 Paris: 2023-05-29 10:15:08
Conclusion
The above output shows the representation of different countries time formats by using strftime(), astimezone(), now(), and timezone(). The utcnow() function shows the universal time standard of different countries. Then we saw there are two modules namely pendulum and arrow that will show the unique solution of the given problem statement.