Strftime function in Python – Python datetime module
|In this Python article, we will discuss everything about strftime
method, along with different types and ways to implement it and some examples to understand the concept better. Let’s get started.
1. What is the use of strftime in Python?
The function or method
strftime()
is used to get the formatted string output of the provided date, time or DateTime object. The output format depends on the explicit format string.
The strftime takes the parameter to directives. The default format is “%a %b %d %H:%M:%S %Y”, its formatting is the same that is returned by ctime()
.
Syntax: obj.strftime(format) -> where obj is date, time or datetime object
2. Different format Code List
Directive | Specification | Output |
---|---|---|
%a | used for an abbreviated name for weekdays | Sun, Mon, …, Sat |
%A | used for the full name of a weekday | Sunday, Monday, …, Saturday |
%w | used to represent the weekday in whole numbers with 0 as of Sunday and 6 as of Saturday and other values in between as different weekdays. | 0, 1, 2, 3, 4, 5, 6 |
%d | used to represent the day of a month in a zero-padded number way. | 01, 02, …, 31 |
%-d | used to represent month day as natural number | 1, 2, …, 30 |
%b | used to represent the abbreviated name for the month. | Jan, Feb, …, Dec |
%B | used to represent the full name for the month. | January, February, …, December |
%m | used to represent the month in a zero-padded number way. | 01, 02 … 12 |
%-m | used to represent month as a natural number | 1, 2, …, 12 |
%y | used to represent a specific year as a zero-padded decimal number without displaying the century. | 01, 02, … 99 |
%-y | used to represent year without century as the whole number | 0,1,2, …, 98,99 |
%Y | used to represent a specific year as a decimal number with displaying the century. | 0001, 0002, … , 9999 |
%H | used to represent the hour in the 24-hour clock format in a zero-padded decimal number way. | 01, 02, … , 23 |
%I | used to represent the hour in the 12-hour clock format in a zero-padded decimal number way. | 01, 02, … , 12 |
%-I | used to represent time in 12-hour format | |
%p | used to represent AM or PM. | AM, PM (en_US) |
%M | used to represent the minute in a zero-padded decimal number way. | 01, 02, … , 59 |
%-M | used to represent minute as the whole number | 0, 1, …, 59 |
%S | used to represent the second in a zero-padded decimal number way. | 01, 02, … , 59 |
%-S | used to represent second as the whole number. | 0, 1, …, 59 |
%f | used to represent the Microsecond in a zero-padded decimal number way to its left. | 000000, 000001, …, 999999 Not applicable with time module. |
%z | used to represent the ±HHMM[SS] calculated from the UTC according to the local time. (naive object returns an empty string). | (empty), +0000, -0400, +1030 |
%Z | used to represent the name of the time zone. | (empty), UTC, IST, CST |
%j | used to represent the particular day of the year in a zero-padded decimal number way. | 001, 002, …, 366 |
%U | used to represent the particular week number of the year in a zero-padded decimal number way. After a new year, days after the first Sunday are marked to be in week 0. | 00, 01, …, 53 |
%W | used to represent the particular week number of the year in a zero-padded decimal number way. After a new year, days after the first Monday are marked to be in week 0. | 00, 01, …, 53 |
%c | used to represent the local appropriate date and time. | Tue Aug 16 21:30:00 1988 |
%x | used to represent the local appropriate date. | 08/16/88 (None) 08/16/1988 |
%X | used to represent the local appropriate time. | 21:30:00 |
%% | used to represent a “%” character as a literal. | % |
3. strftime examples in Python
Let’s take some examples in different programs to see how to use them.
import datetime timenow = datetime.datetime. now() print("Original time now, with no formatting is: ", timenow) # 1 form1 = timenow.strftime("%a %m %y") print('\noutput of format 1:', form1) # 2 form2 = timenow.strftime("%A %-m %Y") print('\noutput of format 2:', form2) # 3 form3 = timenow.strftime("%-I %p %S") print('\noutput of format 3:', form3) # 4 form4 = timenow.strftime("%-j") print('\noutput of format 4:', form4)
Output Original time now, with no formatting is: 2021-07-11 04:08:26.319320 output of format 1: Sun 07 21 output of format 2: Sunday 7 2021 output of format 3: 4 AM 26 output of format 4: 192
import datetime stamp_for_time = 1637319991 timenow = datetime.datetime.fromtimestamp(stamp_for_time) format1 = timenow.strftime("%c") print("Output 1:", format1) format2 = timenow.strftime("%x") print("Output 2:", format2) format3 = timenow.strftime("%X") print("Output 3:", format3)
Output Output 1: Fri Nov 19 11:06:31 2021 Output 2: 11/19/21 Output 3: 11:06:31
import datetime timestamp = 1628936199 timenow = datetime.datetime.fromtimestamp(timestamp) # 1 form1 = timenow.strftime("%c") print('\noutput of format 1:', form1) # 2 form2 = timenow.strftime("%x") print('\noutput of format 2:', form2) # 3 form3 = timenow.strftime("%X") print('\noutput of format 3:', form3)
Output output of format 1: Sat Aug 14 10:16:39 2021 output of format 2: 08/14/21 output of format 3: 10:16:39
4. Conclusion
Finally, if we sum up, in this article we learned everything about the strftime function/method.
We covered how to use and why to use the strftime function and different input formats, we also covered:
- What is the use of Strftime in python?
- Different format Code List and some of its example.
Read More: We recommend going through another article to convert a string to DateTime using strptime()
Helpful Links
Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.
Also for examples in Python and practice please refer to Python Examples.
Complete code samples are present on Github project.
Recommended Books
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding
Do not forget to share and Subscribe.
Happy coding!! ?