问题:在Python中,如何以可读格式显示当前时间

如何将当前时间显示为:

12:18PM EST on Oct 18, 2010

在Python中 谢谢。

How can I display the current time as:

12:18PM EST on Oct 18, 2010

in Python. Thanks.


回答 0

首先是快速而肮脏的方法,其次是精确的方法(识别日光的节省与否)。

import time
time.ctime() # 'Mon Oct 18 13:35:29 2010'
time.strftime('%l:%M%p %Z on %b %d, %Y') # ' 1:36PM EDT on Oct 18, 2010'
time.strftime('%l:%M%p %z on %b %d, %Y') # ' 1:36PM EST on Oct 18, 2010'

First the quick and dirty way, and second the precise way (recognizing daylight’s savings or not).

import time
time.ctime() # 'Mon Oct 18 13:35:29 2010'
time.strftime('%l:%M%p %Z on %b %d, %Y') # ' 1:36PM EDT on Oct 18, 2010'
time.strftime('%l:%M%p %z on %b %d, %Y') # ' 1:36PM EST on Oct 18, 2010'

回答 1

您所需要的只是文档中

import time
time.strftime('%X %x %Z')
'16:08:12 05/08/03 AEST'

All you need is in the documentation.

import time
time.strftime('%X %x %Z')
'16:08:12 05/08/03 AEST'

回答 2

您可以执行以下操作:

>>> from time import gmtime, strftime
>>> strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
'Thu, 28 Jun 2001 14:17:15 +0000'

有关%代码的完整文档位于http://docs.python.org/library/time.html

You could do something like:

>>> from time import gmtime, strftime
>>> strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
'Thu, 28 Jun 2001 14:17:15 +0000'

The full doc on the % codes are at http://docs.python.org/library/time.html


回答 3

import time
time.strftime('%H:%M%p %Z on %b %d, %Y')

这可能会派上用场:http : //strftime.org/

import time
time.strftime('%H:%M%p %Z on %b %d, %Y')

This may come in handy


回答 4

通过使用此代码,您将获得实时时区。

import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))

By using this code, you’ll get your live time zone.

import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))

回答 5

看一下http://docs.python.org/library/time.html提供的功能

您在那里有几个转换功能。

编辑:请参阅datetime(http://docs.python.org/library/datetime.html#module-datetime),以获得更多类似OOP的解决方案。time上面链接的库有点必要。

Take a look at the facilities provided by the time module

You have several conversion functions there.

Edit: see the module for more OOP-like solutions. The time library linked above is kinda imperative.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。