Home Python Datetime Methods strftime() Python datetime.strftime() Method Definition strftime(format) ("string format time") returns a str built from the format string, where each % code is replaced by part of the date or time: %Y by the year, %m by the month, and so on. Other characters are copied as they are.
The same codes work inside f-strings and str.format() : f"{moment:%d %b %Y}" calls strftime() for you. To go the other way, from text to a datetime, use strptime() .
datetime.strftime() is an instance method of datetime.datetime: call it on an existing object with dot notation, such as moment.strftime(...). Import the class first, for example from datetime import datetime. The date and time classes have a method with the same name.
Syntax Call it on a date, time, or datetime with a format string.
text = moment.strftime(format)
text = f"{moment:%Y-%m-%d}" Call What it does datetime(2024, 3, 15, 9, 5).strftime("%Y-%m-%d %H:%M")Returns '2024-03-15 09:05'. date(2024, 3, 15).strftime("%d/%m/%Y")Returns '15/03/2024'. datetime(2024, 3, 15, 21, 0).strftime("%I %p")Returns '09 PM' in the C/English locale.
Write %% for a literal percent sign. Codes for fields an object does not have print zeros: a date formats %H as 00.
Method signatures datetime.strftime(format)
date.strftime(format)
time.strftime(format)
# format: str of literal text and % codes; returns: str Parameters Parameter Description format A str such as "%Y-%m-%d %H:%M:%S". See the code table on the Python Datetime hub page.
Return Value Return Value Description str The formatted text.
Important
%a, %A, %b, %B, and %p follow the current locale, so output can change on a machine set to another language. Platform-only codes such as %-d (Linux, macOS) and %#d (Windows) are not portable. On a naive value %z and %Z produce empty strings. A non-str format raises TypeError.
Python Examples 1. Numeric codes 2. Names and week numbers 3. Zone codes and f-strings Example 1 — Format Dates and Times with Numeric Codes Use the codes for year, month, day, 24-hour and 12-hour clocks, microseconds, and day of the year on one fixed moment.
from datetime import datetime
moment = datetime(2024, 3, 15, 9, 5, 7, 42000)
print(moment.strftime("%Y-%m-%d %H:%M:%S"))
print(moment.strftime("%d/%m/%y %I:%M %p"))
print(moment.strftime("%H:%M:%S.%f"))
print(moment.strftime("Day %j of %Y, 100%% done")) 2024-03-15 09:05:07
15/03/24 09:05 AM
09:05:07.042000
Day 075 of 2024, 100% done How It Works Zero padding. %m, %H, %M, and %S always print two digits, so 9:05:07 becomes 09:05:07.Short year and 12-hour clock. %y prints 24, and %I %p prints 09 AM (C/English locale).Six-digit fraction. %f pads the 42000 microseconds to 042000.Day of the year. %j counts from 1 January, giving 075 for 15 March in a leap year; %% prints one percent sign.Example 2 — Print Day Names, Month Names, and Week Numbers Format 1 January 2023, a Sunday, to see full and short names and how %U and %W count weeks differently. Names assume the default C/English locale.
from datetime import date
day = date(2023, 1, 1)
print(day.strftime("%A, %B %d, %Y"))
print(day.strftime("%a %b %d"))
print(day.strftime("%U %W"))
print(day.strftime("%w"), day.weekday()) Sunday, January 01, 2023
Sun Jan 01
01 00
0 6 How It Works Full and short names. %A and %B give the full weekday and month; %a and %b give three-letter forms.Sunday-first weeks. %U starts week 1 on the first Sunday, which is this very day, so it prints 01.Monday-first weeks. %W has not reached its first Monday yet, so the day belongs to week 00.Two weekday systems. %w counts Sunday as 0, while weekday() counts Monday as 0, giving 6 for Sunday.Example 3 — Show Offsets with %z and %Z Compare a naive and an aware value, convert to UTC, and format with an f-string.
from datetime import datetime, timedelta, timezone
ist = timezone(timedelta(hours=5, minutes=30))
naive = datetime(2024, 3, 15, 10, 30)
aware = naive.replace(tzinfo=ist)
print(repr(naive.strftime("%z|%Z")))
print(aware.strftime("%z|%Z"))
print(aware.astimezone(timezone.utc).strftime("%H:%M %z %Z"))
print(f"{aware:%Y-%m-%d %H:%M}") '|'
+0530|UTC+05:30
05:00 +0000 UTC
2024-03-15 10:30 How It Works Naive means empty. Without a tzinfo both zone codes become empty strings, leaving only the |.Offset and name. %z prints +0530; %Z prints the zone name, which an unnamed offset generates as UTC+05:30.UTC version. After astimezone() , the offset is +0000 and the name is UTC.Format spec shortcut. In an f-string, the text after the colon is passed to strftime().Frequently Asked Questions How do I format a datetime as a string in Python? Call moment.strftime("%Y-%m-%d %H:%M:%S"), or use an f-string such as f"{moment:%Y-%m-%d}".
How do I remove the leading zero from the day? %-d works on Linux and macOS and %#d on Windows, but neither is portable. A portable way is f"{moment.day}/{moment.month}/{moment.year}".
Why is %Z empty? The datetime is naive. Attach a tzinfo with replace(tzinfo=...) or create it aware.
Why are month names not in English on my server? %b, %B, %a, and %A follow the active locale. Use numeric codes or ISO 8601 when text must be machine-readable.
What is the difference between strftime and strptime? strftime formats a datetime into text; strptime parses text into a datetime. They share the same codes.
Next: datetime.strptime() Classmethod: parse a string into a datetime using format codes like %Y-%m-%d.
strptime() → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
13 people found this page helpful
Helpful Share Copy link Suggestion