Python程式:以不同國家格式顯示日期
在Python中,我們有一些內建的函式,例如`datetime.now()`、`utcnow()`、`astimezone()`和`strftime()`,可以用來以不同國家的格式顯示時間。在這個程式中,我們將使用`datetime`模組(訪問所有與日期和時間相關的內容)和`pytz`模組(允許時區計算和`datetime`的轉換)。為了獲取不同國家的時區,UTC將由名為`utcnow()`的內建函式設定。問題陳述遵循Python的一些內建函式,例如`datetime.now()`、`utcnow()`、`astimezone()`和`strftime()`,這些函式將用於以不同國家的格式顯示時間。
語法
以下語法在示例中使用:
datetime.now()
這是Python中用於查詢當前日期和時間的內建時間函式。
utcnow()
這是一個內建函式,返回UTC時區的時間。
astimezone()
這是一個內建函式,返回`datetime`和指定的時區引數。
strftime()
`strftime()`函式是一個內建函式,接受引數來宣告時間的格式。
now()
這是Python中的一個內建函式,主要用於查詢當前時間和日期。
date.to('Country_time_zone').format('YYYY-MM-DD HH:mm:ss'))
上述語法在示例3中使用:
`date.to('Country_time_zone')`:`date`是將值設定為帶有箭頭模組的`utcnow()`的變數的名稱。`Country_time_zone`設定為國家的時區。例如,印度 - Asia/Kolkata,中國 - Asia/Shanghai等。
`format('YYYY-MM-DD HH:mm:ss')`:`format`主要用於設定國家的日期和時間格式。
示例1
在下面的示例中,我們將首先匯入`datetime`和`pytz`模組。然後,我們建立一個名為`get_utc`的變數來使用名為`datetime.utcnow()`的內建函式儲存`datetime`的值,該函式將提供世界標準時間。然後,為不同的國家/地區建立變數(例如`get_countries_name`),並使用名為`astimezone()`和`timezone()`的內建函式以及名為`pytz`的模組設定值。接下來,將時間的格式儲存在`format_str`變數中。此時間格式作為引數傳遞給`strftime()`函式,以格式化不同國家/地區的時間。然後,建立不同國家/地區的時間變數,並使用`strftime()`函式透過時間的格式設定值。最後,我們使用不同國家/地區的時間變數列印變數。
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 }" )
輸出
**************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
示例2
安裝要求
pip install pendulum
將此命令安裝到您的系統以執行基於pendulum模組的程式。
將此命令安裝到您的系統以執行基於pendulum模組的程式。
在下面的示例中,我們將首先匯入名為`pendulum`的模組。然後使用內建方法`now()`,該方法將與`pendulum`變數一起用於在`date`變數中查詢當前時間和日期。接下來,我們使用多個`print`函式,將不同國家/地區的時區設定為結果。
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())
輸出
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
示例3
安裝要求
pip install arrow
將此命令安裝到您的系統以執行基於arrow模組的程式。
在下面的示例中,我們首先匯入名為`arrow`的模組。然後使用`arrow`模組中的內建方法`utcnow()`,該方法將查詢不同國家/地區的日期和時間並將其儲存在`date`變數中。接下來,使用`to()`和變數`date`設定日期,這將查詢所有不同國家/地區的日期,並進行格式設定以獲取時間並使用`print()`函式獲取結果。
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'))
輸出
*****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
結論
上述輸出顯示了使用`strftime()`、`astimezone()`、`now()`和`timezone()`表示不同國家/地區的時間格式。`utcnow()`函式顯示不同國家/地區的世界標準時間。然後我們看到有兩個模組,即`pendulum`和`arrow`,它們將顯示給定問題陳述的唯一解決方案。
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP