How to parse str(my_datetime) using strptime ?

假装没事ソ 提交于 2019-11-29 14:37:36

Note that str(d) is documented as being equivalent to d.isoformat(' '). This starts with %Y-%m-%d %H:%M:%S (2017-01-01 00:00:00), but then:

  • Either has nothing or .%f for microseconds; and
  • Either has nothing or an offset with a colon, which doesn't match %z.

.strptime doesn't have support for optional parts, therefore there isn't a single format parameter that can match all of these possible outputs, and doesn't support the colon in the offset, so some can't be handled at all.

It is not possible using strptime, as has been explained here.

Upgrade to Python 3.7, and use the datetime.fromisoformat method.

Contributed by Paul Ganssle in issue15873.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!