Python Datetime Strftime()

By | August 5, 2019

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

Python strftime()

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

When we use datetime module to print date and time, it returns a DateTime object which prints the date and time in 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 be also used to 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  current_date_time variable which is actually 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 code.

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

Working of strftime() method:

As we know that strftime works on a datetime object, for that we first create a datetime object using datetime module.

From that datetime object strftime() method grab date and time and place date and time component according to the passed format code.

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

Lets 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 abbreviate of weekdays name. Sun, Mon, …
%A Provide full weekday name. Sunday, Monday, …
%w Provide weekday were Sunday represent 0 and Saturday represent 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 Year without century as a zero-padded decimal number. 00, 01, …, 99
%-y Year without century as a decimal number. 0, 1, …, 99
%Y 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 to be 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 to be 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

Python Tutorials


Introduction

Flow Control

Functions

Datatypes

File Handling

Object & Class

Additional Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *