<pre><code>"timeFrom": "2016-01-15T01:35:30.314Z"
"timeTo": "2016-01-15T01:35:30.314Z"
</code></pre>
<p>如果它们是完全相同的时间,持续时间将非常接近于零(因此为非正)</p>
<hr/>
<p>如果您只是想知道格式,以便传入更合适的值,那么下面的内容会有所帮助。时间字符串的组成部分包括:</p>
<pre><code>2016-01-15T01:35:30.314Z
|||| || || || || || |||| |
|||| || || || || || |||+- UTC timezone
|||| || || || || || +++ milliseconds
|||| || || || || ++ seconds
|||| || || || ++ - minutes
|||| || || ++ hours
|||| || ++ - day of month
|||| ++ month
++++ - year
</code></pre>
<p>因此,如果您想添加一个小时(例如),只需传入:</p>
<pre><code>2016-01-15T02:35:30.314Z
</code></pre>
<hr/>
<p>相反,如果您想使用Python动态修改字符串,它非常“简单”,如下所示:</p>
<pre><code>import datetime as dt
str1 = "2016-01-15T01:35:30.314Z"
fmt = '%Y-%m-%dT%H:%M:%S.%f%z'
print(str1)
str2 = (dt.datetime.strptime(str1.replace('Z','000+0000'),fmt) + dt.timedelta(hours=1)).strftime(fmt).replace('000+0000','Z')
print(str2)
</code></pre>
<p>此时我的输出:</p>
<pre><code>2016-01-15T01:35:30.314Z
2016-01-15T02:35:30.314Z
^
note this bit
</code></pre>
<p>其实质是<code>str2</code>的设置,基本上可以归结为:</p>
<ul>
<li>更改字符串,使<code>strptime</code>高兴(第一个<code>replace</code>)</李>
<li>使用<code>strptime</code>创建<code>datetime</code>对象</李>
<li>使用<code>timedelta</code>为该对象添加一小时,您可以为其指定各种持续时间</李>
<li>用<code>strftime</code>将其转换回字符串</李>
<li>使用第二个<code>replace</code>修改字符串以匹配原始格式</李>
</ul>
<p>请注意,这只适用于<code>Z</code>时区说明符,如果要处理其他时区,则需要指定它们是什么</p>