【问题标题】:In Python, how to display current time in readable format在 Python 中,如何以可读格式显示当前时间
【发布时间】:2011-04-27 01:41:11
【问题描述】:

如何将当前时间显示为:

12:18PM EST on Oct 18, 2010

在 Python 中。谢谢。

【问题讨论】:

  • 抱歉,我认为扩展网站上的问答数据库会有所帮助。以前没有人问过这样的问题。
  • @ensnare:每一个似乎都已经回答了这个问题:stackoverflow.com/search?q=%5Bpython%5D+time+format。你是在暗示你的问题有什么特别之处吗?如果是这样,是什么让您的问题与众不同?
  • @S.Lott,很抱歉,我没有看到最后一个链接。
  • @ensnare 不要道歉,伙计,TFM 就是这样!几乎每个问题和答案都有微妙而有价值的差异。问题越多越好。

标签: python datetime time


【解决方案1】:

看看time模块提供的设施

那里有几个转换函数。

编辑:请参阅datetime 模块了解更多类似 OOP 的解决方案。上面链接的time 库是必不可少的。

【讨论】:

    【解决方案2】:

    您只需要in the documentation

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

    【讨论】:

      【解决方案3】:

      你可以这样做:

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

      【讨论】:

        【解决方案4】:

        首先是快速而肮脏的方式,其次是精确的方式(是否识别夏令时)。

        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'
        

        【讨论】:

        • %z 产生偏移量,例如,-0400%Z 自动为您提供当前时区名称(或缩写)。 Python 不支持%l,你可以改用%I
        • %Z 给我“东部时区”我怎样才能得到 'est' 或 'EST' 的缩写
        【解决方案5】:
        import time
        time.strftime('%H:%M%p %Z on %b %d, %Y')
        

        This may come in handy

        【讨论】:

          【解决方案6】:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-09-15
            • 2017-04-03
            • 2018-04-07
            • 1970-01-01
            相关资源
            最近更新 更多