📜  Python datetime.tzname() 方法示例

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

Python datetime.tzname() 方法示例

tzname()方法用于 DateTime 模块的 DateTime 类。此方法用于以字符串返回传递的 DateTime 对象的时区名称。

句法:

返回类型:

它将以字符串返回传递的 DateTime 对象的时区名称。



所以首先我们需要导入 DateTime 模块,为了获得确切位置的时区,我们只需将 datetime.now()函数生成的 DateTime 标记传递给 tzname()函数。

示例 1:将返回 tzinfo() 的Python程序,它是 none

Python3
# import datetime module
from datetime import datetime
  
# import pytz module
import pytz
  
# get the datetime of the present
dt = datetime.now()
  
# Tzinfo is missing from the time object
print(dt)
  
# display tz info for the dt
print(dt.tzinfo)
print("Timezone:", dt.tzname())
print()


Python3
# import datetime module
from datetime import datetime
  
# import pytz module
import pytz
  
# get the datetime of the present
dt = datetime.now()
  
# Tzinfo is missing from the time object
print(dt)
  
# display tz info for the dt
print(dt.tzinfo)
print("Timezone:", dt.tzname())
print()
  
# Adding a timezone for asia /kolkate
timezone = pytz.timezone("Asia/Kolkata")
  
# getting the timezone using localize method
mydt = timezone.localize(dt)
  
print(mydt)
  
# getting the time zone info using tzinfo method
print("Tzinfo:", mydt.tzinfo)
  
# display time zone name using tzname
print("Timezone name:", mydt.tzname())


Python3
# import datetime module
from datetime import datetime
  
# import pytz module
import pytz
  
# get the datetime of the present
dt = datetime.now()
  
# Tzinfo is missing from the time object
print(dt)
  
# display tz info for the dt
print(dt.tzinfo)
print("Timezone:", dt.tzname())
print()
  
# Adding a timezone for asia /Tokyo
timezone = pytz.timezone("Asia/Tokyo")
  
# getting the timezone using localize method
mydt = timezone.localize(dt)
  
print(mydt)
  
# getting the time zone info using tzinfo method
print("Tzinfo:", mydt.tzinfo)
  
# display time zone name using tzname
print("Timezone name:", mydt.tzname())


输出:

也可以通过显式添加时区来获取其他地方的时区。

示例 2:为亚洲/加尔各答添加时区并获取时区详细信息的Python程序



蟒蛇3

# import datetime module
from datetime import datetime
  
# import pytz module
import pytz
  
# get the datetime of the present
dt = datetime.now()
  
# Tzinfo is missing from the time object
print(dt)
  
# display tz info for the dt
print(dt.tzinfo)
print("Timezone:", dt.tzname())
print()
  
# Adding a timezone for asia /kolkate
timezone = pytz.timezone("Asia/Kolkata")
  
# getting the timezone using localize method
mydt = timezone.localize(dt)
  
print(mydt)
  
# getting the time zone info using tzinfo method
print("Tzinfo:", mydt.tzinfo)
  
# display time zone name using tzname
print("Timezone name:", mydt.tzname())

输出:

示例 3:获取 asia/Tokyo 的时区详细信息

蟒蛇3

# import datetime module
from datetime import datetime
  
# import pytz module
import pytz
  
# get the datetime of the present
dt = datetime.now()
  
# Tzinfo is missing from the time object
print(dt)
  
# display tz info for the dt
print(dt.tzinfo)
print("Timezone:", dt.tzname())
print()
  
# Adding a timezone for asia /Tokyo
timezone = pytz.timezone("Asia/Tokyo")
  
# getting the timezone using localize method
mydt = timezone.localize(dt)
  
print(mydt)
  
# getting the time zone info using tzinfo method
print("Tzinfo:", mydt.tzinfo)
  
# display time zone name using tzname
print("Timezone name:", mydt.tzname())

输出: