在Python中获取类似PHP的格式化日期时间

3 投票
4 回答
5690 浏览
提问于 2025-04-16 04:04

如何在Python中获取格式化的日期和时间,和PHP中的date('M d Y', $timestamp);一样?

4 个回答

2

日期、日期时间和时间对象都有一个叫做 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 中可以用日期时间和时间模块做的事情。

    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
4

你可以使用一个合适的 strftime 函数。下面是一个使用 datetime 对象的例子。

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

在编程中,有时候我们需要处理一些数据,这些数据可能来自不同的地方,比如用户输入、文件或者网络请求。为了让程序能够理解这些数据,我们需要把它们转换成程序能处理的格式。

比如说,如果你从一个网站上获取了一些信息,这些信息可能是以文本的形式存在的。为了让程序能够使用这些信息,我们需要把它们转化为程序能理解的对象或者变量。

这个过程就像是把一种语言翻译成另一种语言,让不同的系统能够互相理解。这样,程序才能顺利地运行,完成我们想要的功能。

总之,处理数据的关键在于理解如何将不同格式的数据转换为程序可以使用的形式,这样才能让我们的程序更加智能和高效。

>>> import time
>>> timestamp = 1284375159
>>> time.strftime("%m %d %Y",time.localtime(timestamp))
'09 13 2010'

撰写回答