python - python datetime function for different type of timezone
问题描述
I would like to write a function different type of time zone. In this example I am in Ontario and want to winnipeg time.
def winnipeg_time():
x = datetime.datetime.now('America/Winnipeg')
print ('x')
Error I get : tzinfo argument must be None or of a tzinfo subclass, not type 'str
解决方案
您可以使用 指定tzinfo
子类gettz()
。要考虑的另一个选项是用于astimezone()
指定时区。
以下是这两个选项的示例:
from datetime import datetime
import pytz
from dateutil.tz import tz
aw = pytz.timezone('America/Winnipeg')
x = datetime.now().astimezone(aw)
print(f'The pytz datetime in Winnipeg tz is {x}')
x2 = datetime.now(tz.gettz('America/Winnipeg'))
print(f'The gettz datetime in Winnipeg tz is {x2}')
结果:
The pytz datetime in Winnipeg tz is 2020-04-20 13:54:27.600227-05:00
The gettz datetime in Winnipeg tz is 2020-04-20 13:54:27.600544-05:00
推荐阅读
- javascript - 为什么此代码的输出是 242 而不是 243
- mysql - 如何过滤fname和lname?- mysql
- android - 使用改造安卓将文件上传到服务器?
- excel-formula - Formula To Remove Duplicates based On Two Columns
- oracle - .net 核心实体框架到 Oracle 数据库
- java - How to inject an object passed to the BaseActivity, instantiating it with reflection (or any other way if possible)
- api - 单一 API 端点的优缺点
- java - Referer 标头中的 URL 被检测为使用多种编码
- mysql - Python脚本数据库连接作为池不起作用,但简单连接正在工作
- c# - .NET 核心图像大小调整:“无法加载文件或程序集”