Python
strftime
date formatting
leading zeros
datetime

Python strftime: date or month without a leading zero

Codemia Knowledge Hub ·

To drop the leading zero in Python's strftime, use %-d and %-m on Unix-like systems (Linux, macOS) or %#d and %#m on Windows. Neither is portable, so for cross-platform code the safest answer is to format the parts yourself, for example f"{dt.month}/{dt.day}/{dt.year}".

The default: zero-padded %d and %m

The standard directives pad day and month with zeros:

python
1from datetime import datetime
2
3dt = datetime(2026, 3, 7)
4print(dt.strftime("%Y-%m-%d"))
5print(dt.strftime("%m/%d/%Y"))

Output:

text
2026-03-07
03/07/2026

That is often what you want for logs, filenames and stable machine-readable formats.

Linux and macOS: %-d and %-m

On Linux and macOS, the minus flag usually removes zero padding:

python
1from datetime import datetime
2
3dt = datetime(2026, 3, 7)
4print(dt.strftime("%-m/%-d/%Y"))

Typical output:

text
3/7/2026

This is convenient, but it is not guaranteed to behave the same way on Windows.

Windows: %#d and %#m

The Windows strftime implementation commonly uses # instead of - for non-padded output:

python
1from datetime import datetime
2
3dt = datetime(2026, 3, 7)
4print(dt.strftime("%#m/%#d/%Y"))

That often produces the same visible result, but it ties the format string to the platform.

Portable option: format the parts yourself

If portability matters, build the string from the integer date components:

python
1from datetime import datetime
2
3dt = datetime(2026, 3, 7)
4formatted = f"{dt.month}/{dt.day}/{dt.year}"
5print(formatted)

This avoids platform-specific strftime flags entirely.

To get March 7, 2026 without relying on day formatting directives, mix strftime for the text part with the integer day:

python
1from datetime import datetime
2
3dt = datetime(2026, 3, 7)
4formatted = f"{dt.strftime('%B')} {dt.day}, {dt.year}"
5print(formatted)

This is often the most practical solution when only one or two components need non-padded output.

Why the flags differ by platform

In many environments Python's strftime depends on the platform C library. Python exposes the formatting interface, but platforms do not all support the same modifier flags. That is why:

  • %d is broadly portable
  • %-d is common but not universal
  • %#d is a Windows-specific convention

The portability problem comes from the underlying system implementation, not from Python syntax.

A reusable helper

Rather than scattering platform-specific directives through the codebase, write a small helper:

python
1from datetime import date
2
3def format_date_no_leading_zero(value):
4    return f"{value.month}/{value.day}/{value.year}"
5
6
7print(format_date_no_leading_zero(date(2026, 3, 7)))

The behavior is obvious and there is no need to branch on the operating system for common display formats. If month names or localized output matter, you can still use strftime for the text parts and assemble the numbers manually.

When to keep leading zeros

Do not remove padding just because a UI example looks nicer without it. Leading zeros are useful for:

  • lexicographically sortable strings
  • stable log formats
  • file naming
  • machine-readable protocols

For example, 2026-03-07 sorts correctly as a string, while 2026-3-7 does not line up as neatly in fixed-format output. The real question is whether the string should be human-friendly or system-friendly. Avoid platform-specific flags in cross-platform libraries unless you have tests on each platform.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms