擅长:python、mysql、java
<p>在版本3.7中更改:当向strtime()方法提供%z指令时,UTC偏移量可以有冒号作为小时、分钟和秒之间的分隔符。例如,“+01:00:00”将被解析为一小时的偏移量。此外,提供“Z”与“+00:00”相同。你知道吗</p>
<p>python3.6似乎只支持+0000,没有冒号</p>
<p><a href="https://docs.python.org/3.7/library/datetime.html#strftime-and-strptime-behavior" rel="nofollow noreferrer">https://docs.python.org/3.7/library/datetime.html#strftime-and-strptime-behavior</a></p>
<p>这个怎么样:</p>
<pre><code>import datetime
import re
regex = r"([+-])([0-9]{2}):([0-9]{2})"
subst = "\\1\\2\\3"
dateandtime = "2019-07-31 12:02:35+00:00"
dateandtime = re.sub(regex, subst, dateandtime, 0)
print(dateandtime)
onlytime = datetime.datetime.strptime(str(dateandtime),"%Y-%m-%d %H:%M:%S%z")
print(onlytime)
</code></pre>