📌  相关文章
📜  如何在Python转换具有不同时区的日期和时间?

📅  最后修改于: 2022-05-13 01:54:50.206000             🧑  作者: Mango

如何在Python转换具有不同时区的日期和时间?

在本文中,我们将讨论如何在Python转换不同时区的日期和时间。为此,我们将使用Python pytz的模块之一。该模块将 Olson tz 数据库引入Python ,该库允许使用Python进行准确和跨平台的时区计算。方法pytz.timezone()生成特定区域的当前时区。

句法:

示例 1:



在以下程序中,当前 UTC 时间根据亚洲/加尔各答时区进行转换。

Python3
from datetime import datetime
import pytz
  
# get the standard UTC time
UTC = pytz.utc
  
# it will get the time zone
# of the specified location
IST = pytz.timezone('Asia/Kolkata')
  
# print the date and time in
# standard format
print("UTC in Default Format : ",
      datetime.now(UTC))
  
print("IST in Default Format : ",
      datetime.now(IST))
  
# print the date and time in
# specified format
datetime_utc = datetime.now(UTC)
print("Date & Time in UTC : ",
      datetime_utc.strftime('%Y:%m:%d %H:%M:%S %Z %z'))
  
datetime_ist = datetime.now(IST)
print("Date & Time in IST : ",
      datetime_ist.strftime('%Y:%m:%d %H:%M:%S %Z %z'))


Python3
from datetime import datetime
import pytz
  
# get the standard UTC time
original = pytz.timezone('Asia/Kolkata')
  
# it will get the time zone
# of the specified location
converted = pytz.timezone('US/Eastern')
  
# print the date and time in
# specified format
dateTimeObj = datetime.now(original)
print("Original Date & Time: ",
      dateTimeObj.strftime('%Y:%m:%d %H:%M:%S %Z %z'))
  
# converted
dateTimeObj = datetime.now(converted )
print("Converted Date & Time: ",
      dateTimeObj.strftime('%Y:%m:%d %H:%M:%S %Z %z'))


Python3
from datetime import datetime
import pytz
  
# get the standard UTC time
original = pytz.utc
  
# create datetime object
dateTimeObj = datetime.now(original)
print("Original Date & Time: ",
      dateTimeObj.strftime('%Y:%m:%d %H:%M:%S %Z %z'))
  
# it will get the time zone
# of the specified location
for timeZone in pytz.all_timezones:
    if 'Indian/' in timeZone:
        dateTimeObj = datetime.now(pytz.timezone(timeZone))
        print(timeZone,":",dateTimeObj.strftime('%Y:%m:%d %H:%M:%S %Z %z'))


输出:

示例 2:

这是将当前亚洲/加尔各答时区转换为美国/东部时区的另一个程序。

蟒蛇3

from datetime import datetime
import pytz
  
# get the standard UTC time
original = pytz.timezone('Asia/Kolkata')
  
# it will get the time zone
# of the specified location
converted = pytz.timezone('US/Eastern')
  
# print the date and time in
# specified format
dateTimeObj = datetime.now(original)
print("Original Date & Time: ",
      dateTimeObj.strftime('%Y:%m:%d %H:%M:%S %Z %z'))
  
# converted
dateTimeObj = datetime.now(converted )
print("Converted Date & Time: ",
      dateTimeObj.strftime('%Y:%m:%d %H:%M:%S %Z %z'))

输出:



可以通过执行以下代码获取 pytz 中存在的所有时区值:

for timezone in pytz.all_timezones:
     print(timezone) 

以下是将特定时区转换为印度地区多个时区的程序:

蟒蛇3

from datetime import datetime
import pytz
  
# get the standard UTC time
original = pytz.utc
  
# create datetime object
dateTimeObj = datetime.now(original)
print("Original Date & Time: ",
      dateTimeObj.strftime('%Y:%m:%d %H:%M:%S %Z %z'))
  
# it will get the time zone
# of the specified location
for timeZone in pytz.all_timezones:
    if 'Indian/' in timeZone:
        dateTimeObj = datetime.now(pytz.timezone(timeZone))
        print(timeZone,":",dateTimeObj.strftime('%Y:%m:%d %H:%M:%S %Z %z'))

输出: