如何在Python中转换具有不同时区的日期和时间
在本文中,我们将展示如何在Python中转换具有不同时区的日期和时间。
- 使用astimezone()函数
-
使用datetime.now()函数
在Python日期和时间中处理时区最简单的方法是使用pytz模块。这个库允许准确和跨平台的时区计算。pytz将Olson时区数据库引入Python。它还解决了夏令时结束时模糊时间的问题,您可以在Python库参考(datetime.tzinfo)中了解更多详情。
在使用之前,您需要安装pytz库:
pip install pytz
方法1:使用 astimezone() 函数
步骤
下面是执行所需任务的算法/步骤:
- 使用 import 关键字导入 datetime 模块(Python 中有一个叫做 datetime 的模块来处理日期和时间)。
-
使用 import 关键字从 pytz 模块中导入 timezone。
-
使用 input() 函数将日期格式作为字符串输入,并创建一个变量来存储它。
-
使用 datetime.now() 函数获取当前时间(返回当前本地日期和时间),并使用 timezone() 函数将时区作为参数传递给它,如 ‘UTC’。
-
使用 strftime() 对上述日期进行格式化,并打印出来。
-
使用 datetime.astimezone() 函数将上述时间转换为另一个时区(datetime.astimezone() 函数用于修改 datetime 模块中 datetime 类的对象),并将新的时区作为参数传递给它。
-
使用 strftime() 对上述转换后的日期进行格式化,并打印出来。
示例
以下程序返回当前时间,并将其转换为另一个时区使用 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对象进行格式化并打印出来。
示例
以下程序使用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对象。