【问题标题】:Getting formatted datetime in Python like in PHP像在 PHP 中一样在 Python 中获取格式化的日期时间
【发布时间】:2011-04-11 15:16:50
【问题描述】:

如何在 Python 中以与 PHP 相同的方式获取格式化的日期时间 date('M d Y', $timestamp);?

【问题讨论】:

标签: php python datetime time


【解决方案1】:

日期、日期时间和时间对象都支持 strftime(format) 方法,在显式格式字符串的控制下创建表示时间的字符串。

这里是格式代码及其指令和含义的列表。

    %a  Locale’s abbreviated weekday name.
    %A  Locale’s full weekday name.      
    %b  Locale’s abbreviated month name.     
    %B  Locale’s full month name.
    %c  Locale’s appropriate date and time representation.   
    %d  Day of the month as a decimal number [01,31].    
    %f  Microsecond as a decimal number [0,999999], zero-padded on the left
    %H  Hour (24-hour clock) as a decimal number [00,23].    
    %I  Hour (12-hour clock) as a decimal number [01,12].    
    %j  Day of the year as a decimal number [001,366].   
    %m  Month as a decimal number [01,12].   
    %M  Minute as a decimal number [00,59].      
    %p  Locale’s equivalent of either AM or PM.
    %S  Second as a decimal number [00,61].
    %U  Week number of the year (Sunday as the first day of the week)
    %w  Weekday as a decimal number [0(Sunday),6].   
    %W  Week number of the year (Monday as the first day of the week)
    %x  Locale’s appropriate date representation.    
    %X  Locale’s appropriate time representation.    
    %y  Year without century as a decimal number [00,99].    
    %Y  Year with century as a decimal number.   
    %z  UTC offset in the form +HHMM or -HHMM.
    %Z  Time zone name (empty string if the object is naive).    
    %%  A literal '%' character.

这就是我们可以用 Python 中的 datetime 和 time 模块做的事情

    import time
    import datetime

    print "Time in seconds since the epoch: %s" %time.time()
    print "Current date and time: " , datetime.datetime.now()
    print "Or like this: " ,datetime.datetime.now().strftime("%y-%m-%d-%H-%M")


    print "Current year: ", datetime.date.today().strftime("%Y")
    print "Month of year: ", datetime.date.today().strftime("%B")
    print "Week number of the year: ", datetime.date.today().strftime("%W")
    print "Weekday of the week: ", datetime.date.today().strftime("%w")
    print "Day of year: ", datetime.date.today().strftime("%j")
    print "Day of the month : ", datetime.date.today().strftime("%d")
    print "Day of week: ", datetime.date.today().strftime("%A")

这将打印出如下内容:

    Time in seconds since the epoch:    1349271346.46
    Current date and time:              2012-10-03 15:35:46.461491
    Or like this:                       12-10-03-15-35
    Current year:                       2012
    Month of year:                      October
    Week number of the year:            40
    Weekday of the week:                3
    Day of year:                        277
    Day of the month :                  03
    Day of week:                        Wednesday

【讨论】:

    【解决方案2】:

    早在 2003 年,Simon Willison 就编写了 DateFormat,这是一个小的日期格式翻译类,可以在 Python 中启用 php 样式的日期格式字符串:

    >>> from datetime import datetime
    >>> from DateFormat import DateFormat
    >>> timestamp = 1354633606
    >>> df = DateFormat(datetime.fromtimestamp(timestamp))
    >>> df.format('M d Y')
    'Dec 04 2012'
    

    日期格式在此处可用:http://simonwillison.net/2003/oct/7/dateinpython/

    【讨论】:

      【解决方案3】:
      >>> import time
      >>> timestamp = 1284375159
      >>> time.strftime("%m %d %Y",time.localtime(timestamp))
      '09 13 2010'
      

      【讨论】:

        【解决方案4】:

        您可以使用合适的strftime 函数。这是一个使用 datetime 对象的示例。

        >>> from datetime import datetime
        >>> today = datetime.today()
        >>> today.strftime("%m %d %Y")
        '09 13 2010'
        

        【讨论】:

        • 太好了,谢谢!以及如何从格式为 1284375159 的时间中获得相同的结果?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-08
        • 1970-01-01
        • 2014-10-15
        • 2012-10-06
        相关资源
        最近更新 更多