Skip to content Skip to sidebar Skip to footer

Getting Formatted Datetime In Python Like In Php

How to get formatted date time in Python the same way as in PHP date('M d Y', $timestamp);?

Solution 1:

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

Solution 2:

You can use a suitable strftime function. Here is an example using datetime objects.

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

Solution 3:

The date, datetime, and time objects all support a strftime(format) method, to create a string representing the time under the control of an explicit format string.

Here is a list of the format codes with their directive and meaning.

%a  Locale’s abbreviated weekday name.
    %A  Locale’s full weekday name.      
    %b  Locale’s abbreviated month name.     
    %B  Locale’s fullmonth name.
    %c  Locale’s appropriate dateandtime representation.   
    %d  Dayof the monthas 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  Dayof the yearas a decimal number [001,366].   
    %m  Monthas a decimal number [01,12].   
    %M  Minuteas a decimal number [00,59].      
    %p  Locale’s equivalent of either AM or PM.
    %S  Secondas a decimal number [00,61].
    %U  Week number of the year (Sunday as the firstdayof the week)
    %w  Weekday as a decimal number [0(Sunday),6].   
    %W  Week number of the year (Monday as the firstdayof the week)
    %x  Locale’s appropriate date representation.    
    %X  Locale’s appropriate time representation.    
    %y  Yearwithout century as a decimal number [00,99].    
    %Y  Yearwith century as a decimal number.   
    %z  UTC offsetin the form +HHMM or-HHMM.
    %Z  Time zone name (empty string if the object is naive).    
    %%  A literal '%' character.

This is what we can do with the datetime and time modules in 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")

That will print out something like this:

Timein seconds since the epoch:    1349271346.46Currentdateandtime:              2012-10-0315:35:46.461491Orlike this:                       12-10-03-15-35Currentyear:                       2012Monthofyear:                      October
    Week number of the year:            40
    Weekday of the week:                3Dayofyear:                        277Dayof the month :                  03Dayof week:                        Wednesday

Solution 4:

Way back in 2003, Simon Willison wrote DateFormat, a little date format translation class which enables php-style date format strings in Python:

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

DateFormat is available here: http://simonwillison.net/2003/oct/7/dateinpython/

Post a Comment for "Getting Formatted Datetime In Python Like In Php"