• strftime

    For quick syntax & options help with this function, type >>help (“time.strftime”) . This will list all the basic help that you are in need. If you are looking for a much more advanced option with this function, then refer this table.

    About this function: This function is used to format the date & time returned from gmtime() or localtime() functions.

    Syntax: time.strftime(format, [gmttime() or localtime()])

    where ‘format’ is a combination of the directives listed below and it can be a combination of more than one directive passed as a string parameter. Lets see an example & then the list of all possible directives.

    time.strftime example:


    Note: Second Parameter is optional. If left blank, time from localtime() will be considered.

    Directives & their meaning:

    DerivativeDescriptionExample
    %aWeekday as locale’s abbreviated name.Sun
    %AWeekday as locale’s full name.Sunday
    %wWeekday as a decimal number, where 0 is Sunday and 6 is Saturday.1
    %dDay of the month as a zero-padded decimal number.31
    %-dDay of the month as a decimal number. (Platform specific)31
    %bMonth as locale’s abbreviated name.Jan
    %BMonth as locale’s full name.January
    %mMonth as a zero-padded decimal number.1
    %-mMonth as a decimal number. (Platform specific)1
    %yYear without century as a zero-padded decimal number.17
    %YYear with century as a decimal number.2017
    %HHour (24-hour clock) as a zero-padded decimal number.10
    %-HHour (24-hour clock) as a decimal number. (Platform specific)10
    %IHour (12-hour clock) as a zero-padded decimal number.10
    %-IHour (12-hour clock) as a decimal number. (Platform specific)10
    %pLocale’s equivalent of either AM or PM.AM
    %MMinute as a zero-padded decimal number.5
    %-MMinute as a decimal number. (Platform specific)5
    %SSecond as a zero-padded decimal number.5
    %-SSecond as a decimal number. (Platform specific)5
    %fMicrosecond as a decimal number, zero-padded on the left.0
    %zUTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).
    %ZTime zone name (empty string if the object is naive).CDT
    %jDay of the year as a zero-padded decimal number.365
    %-jDay of the year as a decimal number. (Platform specific)365
    %UWeek number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0.25
    %WWeek number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.25
    %cLocale’s appropriate date and time representation.Sun Jan 31 10:05:05 2017
    %xLocale’s appropriate date representation.1/31/2017
    %XLocale’s appropriate time representation.10:05:05
    %%A literal ‘%’ character.%

    Source Reference: This table is referred from the page strftime.org . This page only has one page with description about this function alone. The author has created this page since a quick reference.

    From Python IDE

    This is also a quick ref image for the command syntax & its parameters.

    Python strftime C Programming

    Python strftime C Programming

    Also Read: Open Text File - Read Write File Operations - Any Programming Language

    C or Python strftime

    Python function strftime() call the same function from the c Library. It is just that it has a slightly different syntax. Other than that, the directives used as parameter are all the same.

    size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)

    Explanation for the Parameters:

    • *str – Pointer to the output string
    • maxsize – Size of the above output string
    • format – same as Python strftime directive
    • *timeptr – Pointer to input calendar time.

    For the struct tm, the structure can be seen in time.h header file.

    Sample code with strftime C

    Reference Links:

    1. Python Software Foundation
    2. strftime.org
    3. C tutorial point

    Some of the codes are copied, tested & modified a bit from the above links & posted here as a collection for a quick reference.


    Previous Post:
    Next Post: