擅长:python、mysql、java
<p>如果确实需要当前时间,只需调用不带参数的<code>formatdate</code>:</p>
<pre><code>>>> from email.Utils import formatdate
>>> formatdate()
'Tue, 10 Aug 2010 20:40:23 -0000'
</code></pre>
<p>但是,如果必须传递参数,则需要输出<code>time.time</code>(自1970年1月1日起的秒数):</p>
<pre><code>>>> import time
>>> formatdate(time.time())
'Tue, 10 Aug 2010 20:41:43 -0000'
</code></pre>
<p>FWIW,<code>datetime.datetime.now()</code>返回一个<code>datetime</code>对象,这不是<code>formatdate</code>所期望的。</p>
<p>编辑以添加:如果您已经有一个datetime对象,则可以将其适当格式化为formatdate:</p>
<pre><code>>>> import datetime
>>> dt = datetime.datetime.now()
>>> formatdate(float(dt.strftime('%s')))
'Tue, 10 Aug 2010 20:46:16 -0000'
</code></pre>
<p>编辑:Alex Martelli注意到strftime的'%s'格式字符串可能无法跨平台移植。一个可能的替代方案是,正如他自己所建议的那样</p>
<pre><code>>>> formatdate(time.mktime(dt.timetuple()))
'Tue, 10 Aug 2010 20:46:16 -0000'
</code></pre>