擅长:python、mysql、java
<p><em>这是给那些被标题画在这里,但却得到了操作问题的答案的用户使用的</em></p>
<h2>TLDR公司</h2>
<p>使用<code>fromtimestamp</code>作为引用<a href="https://docs.python.org/3.6/library/datetime.html#datetime.date.fromtimestamp" rel="nofollow noreferrer">in the documentation</a></p>
<blockquote>
<p>Return the local date corresponding to the POSIX timestamp</p>
</blockquote>
<h2>您有一个POSIX/UNIX/EPOCH时间戳</h2>
<p>下面是一个示例(由python生成)</p>
<pre><code>import datetime as dt
epoch = int(dt.datetime.utcnow().timestamp()) # example output: 1520020585
</code></pre>
<h2>将时间戳转换为日期时间对象</h2>
<p>给定时间戳(例如<code>1520020585</code>),我们使用<code>fromtimestamp</code>将其转换回datetime tz naive对象,如下所示:</p>
<pre><code>import datetime as dt
ts = 1520020585 # remember me? I'm the epoch
dt.datetime.fromtimestamp(ts). # outputs: datetime.datetime(2018, 3, 2, 13, 56, 25)
</code></pre>