【问题标题】:convert an isoformat string to python datetime object [duplicate]将isoformat字符串转换为python datetime对象[重复]
【发布时间】:2011-09-21 13:42:45
【问题描述】:

可能重复:
How to parse ISO formatted date in python?

我有一个 isoformat 日期时间作为字符串,如下例所示 -

'2011-05-31T04:35:33.127-05:00'

将其转换为 python 日期时间对象的最佳方法是什么?我遇到了一些帖子,这些帖子讲述了如何获取 isoformat 字符串,但反过来却没有。

谢谢!!

根据严的评论进行编辑-

>>>import dateutil.parser
>>> d1='2011-05-31T04:35:33.127-05:00'
>>> d2=dateutil.parser.parse(d1)
>>> d2
datetime.datetime(2011, 5, 31, 4, 35, 33, 127000, tzinfo=tzoffset(None, -18000))

我需要获取原始字符串表示的本地时间的日期时间对象。因为我不知道输入日期在哪个时区,所以我不能使用 astimezon 方法。获得它的最佳方法是什么? 谢谢!!

【问题讨论】:

    标签: python datetime


    【解决方案1】:

    有一个 strptime 函数,就像在 C 中一样。 http://docs.python.org/library/datetime.html#datetime.datetime.strptime

    【讨论】:

    • 感谢 letibee 的回复,但不幸的是 datetime.datetime.strptime 不适用于 isoformat 日期字符串。谢谢严,是的,我想要 dateutil 解析器之类的东西。我已经编辑了我原来的问题。有没有办法使用 tzoffset 获取当地时间?
    猜你喜欢
    • 1970-01-01
    • 2019-06-28
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    相关资源
    最近更新 更多