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 |
%a | The abbreviated name for the weekend | Sun, Mon, Tue… |
%A | Weekday as full name | Sunday, Monday |
%w | A weekday is a decimal number where 0 is Sunday, 1 is Monday, and so on. | 0, 1, 2 |
%d | Day of the month as a zero-padded decimal number | 01, 02, …., 31 |
%-d | Day of the month as a decimal number | 1, 2, 3, …, 30 |
%b | The abbreviated name for a month | Dec, Nov. |
%B | Month as full name | December, November. |
%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. | 99, 98, …, 1, 0 |
%Y | The year with century as a decimal number | 2012, 2020 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, 3, 4, .., 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 | +0000 |
%Z | Time zone name. | UTC |
%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 | The 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 |
%c | Locale’s appropriate date and time representation | Sun Dec 7 09:34:23 2021 |
%x | Locale’s appropriate date representation | 06/09/21 |
%X | Locale’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