Python .strftime()
Published Apr 19, 2022Updated May 15, 2024
Contribute to Docs
The strftime() function returns a time string based on one or more formatting codes and a time represented as a tuple or stuct_time format.
Syntax
strftime(format_codes, time_value)
A combination of format_codes are used to format the returned string. The time_value is the time that will be formatted and can be represented as a tuple or struct_time value. If no time_value is provided the function will return the current local time as formatted by the given expression.
A few common codes are:
| Code | Description | Example/Range |
|---|---|---|
%a |
The abbreviated day of the week. | Mon, Tues, etc. |
%A |
The day of the week (full name). | Monday, Tuesday, etc. |
%d |
The day of the month in two decimals. | 01 - 31 |
%Y |
The calendar year represented as four decimals. | 2020 |
%H |
The hour of a 24-hour clock in two decimals. | 00 - 23 |
%I |
The hour of a 12-hour clock in two decimals. | 00 - 12 |
%j |
The day of the year represented as three decimals. | 000 - 366 |
%p |
The locale’s equivalent for before and after midday. | AM/PM |
%x |
The locale’s equivalent of a date. | 01/01/01 |
Example
The strftime() function can be used to translate a time to display the associated date:
import timeprint(time.strftime('%x',time.localtime()))
Codebyte Example
The strftime() function can be used to return the current local time with the day and time formatted for a 12-hour clock.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours