如何在 Python 中轉換不同時區的日期和時間?
在本文中,我們將向您展示如何在 Python 中轉換不同時區的日期和時間。
使用 astimezone() 函式
使用 datetime.now() 函式
在Python 日期和時間中,處理時區最簡單的方法是使用pytz模組。此庫允許進行準確的跨平臺時區計算。pytz將 Olson tz 資料庫引入 Python。它還解決了夏令時結束時出現的時間不明確問題,您可以在 Python 庫參考 (datetime.tzinfo) 中瞭解更多資訊。
在使用它之前,您需要使用以下方法安裝它:
pip install pytz
方法 1:使用 astimezone() 函式
演算法(步驟)
以下是執行所需任務需要遵循的演算法/步驟:
使用import關鍵字匯入datetime (用於處理日期和時間,Python 有一個名為 datetime 的模組)模組。
使用import關鍵字從pytz模組匯入timezone 。
使用 input() 函式以字串形式輸入日期格式,並建立一個變數來儲存它。
使用datetime.now() 函式(返回當前本地日期和時間)獲取當前時間,並將其傳遞給 timezone()函式(獲取特定位置的時區),並將其作為引數傳遞,例如 'UTC'。
使用strftime()格式化上述 DateTime 並列印它。
使用datetime.astimezone()函式(datetime.astimezone()函式用於修改 datetime 模組中 datetime 類的物件)將上述時間轉換為另一個時區,並將新的時區作為引數傳遞給它。
使用strftime()格式化上述轉換後的 DateTime 並列印它。
示例
以下程式在轉換為另一個timezone後返回當前時間,使用astimezone()函式:
# importing datetime from datetime import datetime # importing timezone from pytz module from pytz import timezone # giving the format of datetime format = "%Y-%m-%d %H:%M:%S %Z%z" # getting the current time in UTC timezone now_utc = datetime.now(timezone('UTC')) # Format the above DateTime using the strftime() print('Current Time in UTC TimeZone:',now_utc.strftime(format)) # Converting to Asia/Kolkata time zone now_asia = now_utc.astimezone(timezone('Asia/Kolkata')) # Format the above datetime using the strftime() print('Current Time in Asia/Kolkata TimeZone:',now_asia.strftime(format))
輸出
執行上述程式後,將生成以下輸出:
Current Time in UTC TimeZone: 2022-09-07 04:23:21 UTC+0000 Current Time in Asia/Kolkata TimeZone: 2022-09-07 09:53:21 IST+0530
方法 2:使用 datetime.now() 函式
演算法(步驟)
以下是執行所需任務需要遵循的演算法/步驟:
使用 import 關鍵字匯入datetime (用於處理日期和時間,Python 有一個名為 datetime 的模組)模組。
使用 import 關鍵字從pytz模組匯入timezone 。
將日期時間格式作為字串給出,並將其儲存在變數中。
使用pytz模組的timezone()函式(獲取特定位置的時區)獲取標準的 Asia/Kolkata 時區,並將Asia/Kolkata時區作為引數傳遞給它,並將結果儲存在變數中。
使用pytz模組的timezone()函式獲取標準的 US/Eastern 時區,並將US/Eastern時區作為引數傳遞給它,並將結果儲存在變數中。
使用datetime.now()函式(返回當前本地日期和時間)獲取當前時間,並將上述第一個時區,即 asia/kolkata 時區物件作為引數傳遞給它(這裡它將當前日期和時間轉換為 asia/kolkata 時區)。
使用strftime()函式(根據格式程式碼返回表示 datetime 物件的字串)格式化上述 datetime 物件並列印它。
strftime(format)
使用datetime.now()函式獲取當前時間,並將上述第二個時區,即 'US/Eastern' 時區作為引數傳遞給它(這裡它將當前日期和時間轉換為 'US/Eastern' 時區)。
使用 strftime() 函式格式化上述 datetime 物件並列印它。
示例
以下程式在轉換為另一個timezone後返回當前時間,使用astimezone()函式:
# importing datetime from datetime import datetime # importing pytz module import pytz # giving the format of datetime format = "%Y-%m-%d %H:%M:%S %Z%z" # getting the standard UTC time original_tz = pytz.timezone('Asia/Kolkata') # giving the timezone to which it is to be converted converted_tz = pytz.timezone('US/Eastern') # Getting the current time in the Asia/Kolkata Time Zone datetime_object = datetime.now(original_tz) # Format the above datetime using the strftime() print("Original Date & Time: in Asia/Kolkata ",datetime_object.strftime(format)) # Getting the current time in the US/Eastern Time Zone datetime_object = datetime.now(converted_tz ) # Format the above datetime using the strftime() print("Converted Date & Time: in US/Eastern TimeZone ",datetime_object.strftime(format))
輸出
執行上述程式後,將生成以下輸出:
Original Date & Time: in Asia/Kolkata 2022-09-07 10:00:49 IST+0530 Converted Date & Time: in US/Eastern TimeZone 2022-09-07 00:30:49 EDT-0400
結論
在本文中,我們學習瞭如何在 Python 中轉換不同時區的日期和時間。timedelta()函式用於獲取時區物件。我們學習瞭如何使用 datetime.now() 函式獲取當前日期和時間。我們還學習瞭如何使用strftime()函式格式化 datetime 物件。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP