Pep*_* K. 4 python datetime python-2.6 datetime-format
很简单,但我是一个蟒蛇新手.我正在尝试使用特殊格式打印当前的UTC日期和时间:
Python 2.6.6
import datetime, time
print time.strftime("%a %b %d %H:%M:%S %Z %Y", datetime.datetime.utcnow())
TypeError: argument must be 9-item sequence, not datetime.datetime
Run Code Online (Sandbox Code Playgroud)
Mar*_*ers 12
time.strftime()只需time.struct_time样时间的元组,而不是datetime对象.
>>> import datetime
>>> datetime.datetime.utcnow().strftime("%a %b %d %H:%M:%S %Z %Y")
'Sat Oct 04 13:00:36 2014'
Run Code Online (Sandbox Code Playgroud)
但请注意,在Python 2.6中没有包含时区对象,因此没有打印任何内容%Z.返回的对象datetime.datetime.utcnow()是天真的(没有与之关联的时区对象).
由于您正在使用utcnow(),只需手动包含时区:
>>> datetime.datetime.utcnow().strftime("%a %b %d %H:%M:%S UTC %Y")
'Sat Oct 04 13:00:36 UTC 2014'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12894 次 |
| 最近记录: |