gpt4 book ai didi

python - 如何在 Python 中使用 -0400 时区字符串解析日期?

转载 作者:IT老高 更新时间:2023-10-28 21:34:20 27 4
gpt4 key购买 nike

我有一个格式为“2009/05/13 19:19:30 -0400”的日期字符串。似乎以前版本的 Python 可能已经支持 strptime 中的 %z 格式标记用于尾随时区规范,但 2.6.x 似乎已经删除了它。

将此字符串解析为日期时间对象的正确方法是什么?

最佳答案

您可以使用 dateutil 中的 parse 函数:

>>> from dateutil.parser import parse
>>> d = parse('2009/05/13 19:19:30 -0400')
>>> d
datetime.datetime(2009, 5, 13, 19, 19, 30, tzinfo=tzoffset(None, -14400))

这样你就可以得到一个可以使用的日期时间对象。

作为 answered , dateutil2.0 是为 Python 3.0 编写的,不适用于 Python 2.x。 Python 2.x 需要使用 dateutil1.5。

关于python - 如何在 Python 中使用 -0400 时区字符串解析日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1101508/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com