Python strftime()

It is a special function for converting date and time objects into string form. It comes under the DateTime module. This method can take one or more formatted codes. 

  • Importing the datetime class from datetime module:
from datetime import datetime
  •  Storing the current time is now variable: 
now = datetime.now()
  • We then use now to call the strftime method in order to get the string representation:
s = now.strftime("%d/%m/%Y, %H:%M:%S")

d formats to days
m formats to month
Y formats to year
H formats to hour
M formats to minutes
S formats to seconds

Format Code list:

Directive Description Example
%aThe abbreviated name for the weekendSun, Mon, Tue…
%AWeekday as full nameSunday, Monday
%wA weekday is a decimal number where 0 is Sunday, 1 is Monday, and so on.0, 1, 2
%dDay of the month as a zero-padded decimal number01, 02, …., 31
%-dDay of the month as a decimal number1, 2, 3, …, 30
%bThe abbreviated name for a monthDec, Nov. 
%BMonth as full nameDecember, November. 
%mMonth as a zero-padded decimal number.01, 02, …, 12
%-mMonth as a decimal number1, 2, .., 12
%yA year without a century as a zero-padded decimal number.00, 01, …., 99
%-yA year without a century as a decimal number.99, 98, …, 1, 0
%YThe year with century as a decimal number2012, 2020 etc.
%HHour (24-hour clock) as a zero-padded decimal number.00, 01, .., 23
%-HHour (24-hour clock) as a decimal number.0, 1, …, 23
%IHour (12-hour clock) as a zero-padded decimal number.01, 02, …, 12
%-IHour (12-hour clock) as a decimal number.1, 2, 3, 4, .., 12
%pLocale’s AM or PMAM, PM
%MMinute as a zero-padded decimal number.00, 01, …, 59
%-MMinute as a decimal number.0, 1, .., 59
%SSecond as a zero-padded decimal number.00, 01, …, 59
%-SSecond as a decimal number.0, 1, …, 59
%fMicrosecond as a decimal number, zero-padded on the left. 000000-999999
%zUTC offset in the form HHMM+0000
%ZTime zone name.UTC
%jDay of the year as a zero-padded decimal number.001, 002, …, 366
%-jDay of the year as a decimal number.1, 2, …, 366
%UWeek 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
%WThe week number of the year (Monday as the first year of the week) is a decimal number. All days in a new year preceding the first Monday are considered to be week 0.00, 01, …, 53
%cLocale’s appropriate date and time representationSun Dec 7
09:34:23
2021
%xLocale’s appropriate date representation 06/09/21
%XLocale’s appropriate time representation 09:34:25
%%A literal ‘%’ character.%

Let’s understand this with an example:

Code:

Python Code

from datetime import datetime

# storing current date and time in now variable
now = datetime.now()

# Example 1: 
str = now.strftime("%m/%d/%Y, %H:%M:%S")
print('Example 1: ', str)

# Example 2:
str = now.strftime("%d %b %Y")
print('Example 2: ', str)

# Example 3:
str = now.strftime("%Y-%m-%d")
print('Example 3: ', str)

# Example 4:
str = now.strftime("%c")
print('Example 4: ', str)

# Example 5:
str = now.strftime("%X")
print('Example 5: ', str)

Output:

(‘Example 1: ‘, ’08/05/2022, 17:56:41’)
(‘Example 2: ‘, ’05 Aug 2022’)
(‘Example 3: ‘, ‘2022-08-05’)
(‘Example 4: ‘, ‘Fri Aug 5 17:56:41 2022’)
(‘Example 5: ‘, ’17:56:41’)

Special thanks to Asish Kumar for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article