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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3961581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:34:33  来源:igfitidea点击:

In Python, how to display current time in readable format

pythondatetimetime

提问by ensnare

How can I display the current time as:

如何将当前时间显示为:

12:18PM EST on Oct 18, 2010

in Python. Thanks.

在 Python 中。谢谢。

采纳答案by dr jimbob

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'

回答by slezica

Take a look at the facilities provided by http://docs.python.org/library/time.html

看看http://docs.python.org/library/time.html提供的设施

You have several conversion functions there.

你有几个转换功能。

Edit:see datetime(http://docs.python.org/library/datetime.html#module-datetime) also for more OOP-like solutions. The timelibrary linked above is kinda imperative.

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

回答by Aif

All you need is in the documentation.

您所需要的只是文档中的内容

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

回答by Thomas Ahle

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

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

回答by Bill Reason

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

This may come in handy: http://strftime.org/

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

回答by Muneer Ahmad

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"))