Python datetime.strftime() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
datetime 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.

python
text = moment.strftime(format)
text = f"{moment:%Y-%m-%d}"
CallWhat 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
python
datetime.strftime(format)
date.strftime(format)
time.strftime(format)
# format: str of literal text and % codes; returns: str

Parameters

ParameterDescription
formatA str such as "%Y-%m-%d %H:%M:%S". See the code table on the Python Datetime hub page.

Return Value

Return ValueDescription
strThe formatted text.

Python Examples

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.

python
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"))

How It Works

  1. Zero padding. %m, %H, %M, and %S always print two digits, so 9:05:07 becomes 09:05:07.
  2. Short year and 12-hour clock. %y prints 24, and %I %p prints 09 AM (C/English locale).
  3. Six-digit fraction. %f pads the 42000 microseconds to 042000.
  4. 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.

python
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())

How It Works

  1. Full and short names. %A and %B give the full weekday and month; %a and %b give three-letter forms.
  2. Sunday-first weeks. %U starts week 1 on the first Sunday, which is this very day, so it prints 01.
  3. Monday-first weeks. %W has not reached its first Monday yet, so the day belongs to week 00.
  4. 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.

python
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}")

How It Works

  1. Naive means empty. Without a tzinfo both zone codes become empty strings, leaving only the |.
  2. Offset and name. %z prints +0530; %Z prints the zone name, which an unnamed offset generates as UTC+05:30.
  3. UTC version. After astimezone(), the offset is +0000 and the name is UTC.
  4. Format spec shortcut. In an f-string, the text after the colon is passed to strftime().

Frequently Asked Questions

Call moment.strftime("%Y-%m-%d %H:%M:%S"), or use an f-string such as f"{moment:%Y-%m-%d}".
%-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}".
The datetime is naive. Attach a tzinfo with replace(tzinfo=...) or create it aware.
%b, %B, %a, and %A follow the active locale. Use numeric codes or ISO 8601 when text must be machine-readable.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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