以特殊格式打印当前UTC日期时间 [英] Print current UTC datetime with special format

查看:100
本文介绍了以特殊格式打印当前UTC日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常简单,但是我是python新手。我正在尝试以特殊格式打印当前UTC日期和时间:

Pretty simple, but I'm a python newbie. I'm trying to print the current UTC date AND time with special format:

Python 2.6.6

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


推荐答案

time.strftime()仅需 time.struct_time 之类的时间元组 ,而不是 datetime 对象。

使用 datetime.strftime()方法代替:

Use the datetime.strftime() method instead:

>>> import datetime
>>> datetime.datetime.utcnow().strftime("%a %b %d %H:%M:%S %Z %Y")
'Sat Oct 04 13:00:36  2014'

但请注意,在Python 2.6中不包含时区对象,因此%Z不会打印任何内容;由 datetime.datetime.utcnow()返回的对象是 naive (没有与之关联的时区对象)。

but note that in Python 2.6 no timezone objects are included so nothing is printed for the %Z; the object returned by datetime.datetime.utcnow() is naive (has no timezone object associated with it).

由于您使用的是 utcnow(),因此只需手动包含时区:

Since you are using utcnow(), just include the timezone manually:

>>> datetime.datetime.utcnow().strftime("%a %b %d %H:%M:%S UTC %Y")
'Sat Oct 04 13:00:36 UTC 2014'

这篇关于以特殊格式打印当前UTC日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆