Python Datetime Strftime() - Datetime to String Tutorial

Posted in

Python Datetime Strftime() - Datetime to String Tutorial
vinaykhatri

Vinay Khatri
Last updated on December 14, 2024

    In this tutorial, we will discuss how to change the formatting of default datetime objects and how to convert datetime objects to a string.

    What is Python strftime()?

    strftime() is a DateTime method that applies to DateTime objects so the user can get the date and time format according to convenience rather than the DateTime default date and time.

    When we use the datetime module to print date and time, it returns a DateTime object which prints the date and time in the yyyy-mm-dd HH:MM:SS format, but with the help of strftime, we can print the date-time in different formats. The strftime() method can print only specific details from a DateTime object.

    Let’s understand it with an example: Example:

    from datetime import datetime
    current_date_time = datetime.today()
    current_only_date = current_date_time.strftime('%d / %m / %Y')
    current_only_time = current_date_time.strftime('%H : %M : %S')
    current_only_year =current_date_time.strftime('%Y')
    current_only_hour = current_date_time.strftime('%H')
    print('Date is:',current_only_date)
    print('Time is:',current_only_time)
    print('Year is:',current_only_year)
    print('Hour is:',current_only_hour)

    Output:

    Date is: 03 / 08 / 2019
    Time is: 19 : 39 : 36
    Year is: 2019
    Hour is: 19

    Behind the code

    In the above example, we operate the strftime() method on the current_date_time variable, a datetime object. In strftime() method we have passed format code like %Y (year), %m(month), %d (day), %H(hour), %m(minute) and %S(seconds). strftime() method has many format codes.

    In strftime(), we pass a string and the format code like %Y, %m, %d, and the strftime() assigns the date and time according to the format code.

    Working of strftime() method

    We know that strftime works on a datetime object. We first create a datetime object using the datetime module. From that datetime object strftime() method, grab the date and time and place the date and time component according to the passed format code.

    In the strftime() method, we pass a string along with some format codes for different date and time components so the strftime() method can place different date and time components where the user has passed the format code on the string.

    Let's understand it with an example: Example:

    from datetime import datetime
    today =datetime.today()
    date = today.strftime('year:%Y , Month: %m, day: %d')
    time =today.strftime('%H Hour : %M Minutes')
    print(date)
    print(time)

    Output:

    year:2019 , Month: 08, day: 03
    20 Hour : 01 Minutes

    Strftime() method Format Codes

    Format code Description Output
    %a Provide the abbreviation of weekdays' names. Sun, Mon, ...
    %A Provide full weekday name. Sunday, Monday, ...
    %w Provide a weekday where Sunday represents 0 and Saturday represents 6 0, 1, ..., 6
    %d Day of the month as a zero-padded decimal. 01, 02, ..., 31
    %-d Day of the month as a decimal number. 1, 2, ..., 30
    %b Abbreviated month name. Jan, Feb, ..., Dec
    %B Full month name. January, February, ...
    %m Month as a zero-padded decimal number. 01, 02, ..., 12
    %-m Month as a decimal number. 1, 2, ..., 12
    %y A year without a century as a zero-padded decimal number. 00, 01, ..., 99
    %-y A year without a century as a decimal number. 0, 1, ..., 99
    %Y The year with century as a decimal number. 2013, 2019 etc.
    %H Hour (24-hour clock) as a zero-padded decimal number. 00, 01, ..., 23
    %-H Hour (24-hour clock) as a decimal number. 0, 1, ..., 23
    %I Hour (12-hour clock) as a zero-padded decimal number. 01, 02, ..., 12
    %-I Hour (12-hour clock) as a decimal number. 1, 2, ... 12
    %p Locale’s AM or PM. AM, PM
    %M Minute as a zero-padded decimal number. 00, 01, ..., 59
    %-M Minute as a decimal number. 0, 1, ..., 59
    %S Second as a zero-padded decimal number. 00, 01, ..., 59
    %-S Second as a decimal number. 0, 1, ..., 59
    %f Microsecond as a decimal number, zero-padded on the left. 000000 - 999999
    %z UTC offset in the form +HHMM or -HHMM.
    %Z Time zone name.
    %j Day of the year as a zero-padded decimal number. 001, 002, ..., 366
    %-j Day of the year as a decimal number. 1, 2, ..., 366
    %U Week number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered in week 0. 00, 01, ..., 53
    %W Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered in week 0. 00, 01, ..., 53
    %c Locale’s appropriate date and time representation. Mon Sep 30 07:06:05 2013
    %x Locale’s appropriate date representation. 09/30/13
    %X Locale’s appropriate time representation. 07:06:05
    %% A literal '%' character. %

    Example

    Print date in Alphanumeric

    from datetime import datetime
    today =datetime.today()
    date = today.strftime('%d %B %Y')
    day  =today.strftime('%A')
    time =today.strftime('%H %p : %M ')
    print(date)
    print(day)
    print(time)

    Output:

    03 August 2019
    Saturday
    20 PM : 11

    Conclusion

    With the Python datetime strftime() method, you can change the default date and time formatting, i.e., yyyy-mm-dd HH:MM:SS. Whatever format you want the date or time in, the strftime() method always outputs it as a string. You can refer to the strftime() format code table and print the date and time in the format you want.

    People are also reading:

    Leave a Comment on this Post

    0 Comments