Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to display date in different country format
In Python, we can display dates and times in different country formats using built-in modules like datetime and pytz. These modules provide functions such as datetime.now(), utcnow(), astimezone(), and strftime() to handle timezone conversions and formatting.
Key Functions
datetime.now() ? Returns the current local date and time.
utcnow() ? Returns the current UTC (Coordinated Universal Time) datetime.
astimezone() ? Converts datetime to a specified timezone.
strftime() ? Formats datetime as a string using format codes.
Method 1: Using datetime and pytz
The most common approach uses the datetime and pytz modules for timezone handling ?
import datetime
import pytz
# Get current UTC time
utc_time = datetime.datetime.utcnow()
# Define timezones for different countries
russia_tz = pytz.timezone('Europe/Moscow')
india_tz = pytz.timezone('Asia/Kolkata')
china_tz = pytz.timezone('Asia/Shanghai')
srilanka_tz = pytz.timezone('Asia/Colombo')
# Convert UTC to different timezones
russia_time = utc_time.astimezone(russia_tz)
india_time = utc_time.astimezone(india_tz)
china_time = utc_time.astimezone(china_tz)
srilanka_time = utc_time.astimezone(srilanka_tz)
# Format the time
format_str = '%Y-%m-%d %H:%M:%S %Z%z'
print("Time in Different Countries:")
print(f"Russia: {russia_time.strftime(format_str)}")
print(f"India: {india_time.strftime(format_str)}")
print(f"China: {china_time.strftime(format_str)}")
print(f"Sri Lanka: {srilanka_time.strftime(format_str)}")
Time in Different Countries: Russia: 2024-01-15 15:30:45 MSK+0300 India: 2024-01-15 18:00:45 IST+0530 China: 2024-01-15 20:30:45 CST+0800 Sri Lanka: 2024-01-15 18:00:45 +0530+0530
Method 2: Using pendulum Module
The pendulum module provides a more intuitive API for timezone operations ?
Installation: pip install pendulum
import pendulum
# Get current time
current_time = pendulum.now()
print("Current Time in Different Countries:")
print("New York:", current_time.in_timezone('America/New_York').to_datetime_string())
print("London:", current_time.in_timezone('Europe/London').to_datetime_string())
print("Paris:", current_time.in_timezone('Europe/Paris').to_datetime_string())
print("India:", current_time.in_timezone('Asia/Kolkata').to_datetime_string())
Method 3: Using arrow Module
The arrow module offers another approach with clean syntax ?
Installation: pip install arrow
import arrow
# Get current UTC time
utc_now = arrow.utcnow()
print("Time in Different Timezones:")
print("New York:", utc_now.to('America/New_York').format('YYYY-MM-DD HH:mm:ss'))
print("India:", utc_now.to('Asia/Kolkata').format('YYYY-MM-DD HH:mm:ss'))
print("London:", utc_now.to('Europe/London').format('YYYY-MM-DD HH:mm:ss'))
print("Paris:", utc_now.to('Europe/Paris').format('YYYY-MM-DD HH:mm:ss'))
Common Format Codes
| Code | Description | Example |
|---|---|---|
%Y |
4-digit year | 2024 |
%m |
Month (01-12) | 03 |
%d |
Day (01-31) | 15 |
%H |
Hour (00-23) | 14 |
%M |
Minute (00-59) | 30 |
%S |
Second (00-59) | 45 |
%Z |
Timezone name | IST |
Conclusion
Python provides multiple ways to display dates in different country formats. Use datetime with pytz for standard timezone handling, or consider pendulum and arrow for more intuitive APIs. Always work with UTC time internally and convert to local timezones for display purposes.
