Use strftime date formatting codes

A practical review of strftime format codes: what each directive produces, where output varies by locale and platform, and when a plain ISO 8601 date is the safer choice.

strftime is a function that turns a date-and-time value into a text string by following a format pattern. The pattern mixes literal characters with directives that begin with a percent sign, such as %Y for a four-digit year or %B for a full month name. The function originated in the C standard library and also appears in POSIX systems, Python, Ruby, PHP, and many other environments. This review covers what the codes actually produce, where their output becomes unreliable, and when a plain, fixed date format is the better choice.

Is strftime formatting suitable for your use?

strftime is a good fit when you need a date rendered in one predictable layout, for example in a log file, a filename, a report header, or a user-facing label. Its main benefit is precise control. You decide the field order, padding, separators, and whether to include names or numbers.

The main tradeoff is that some codes produce different output depending on the locale and the platform. Codes that output names or locale-preferred layouts, such as %a, %b, %p, %c, and %x, can change when the program runs under a different language setting. Several convenient extensions, such as removing leading zeros, are not available on every system.

A fixed numeric format is preferable when another program needs to read the date, when the audience is international, or when the text needs to sort correctly. The usual choice is the ISO 8601 layout built from %Y-%m-%d, with an explicit time zone offset if time matters. Use locale-dependent codes only for display to a reader whose locale you control or intend to follow.

What strftime actually changes

strftime does not restyle or reorder existing text. It reads the components of a date value, including year, month, day, hour, minute, second, weekday, and, where available, time zone, and writes new characters that represent those components. The result is an ordinary stored string. It is not a visual effect, a font, or a CSS presentation, so what you see is the actual sequence of characters that gets saved, copied, or transmitted.

The transformation can lose information. If the pattern leaves out the year, the time zone, or the AM/PM marker, the string no longer identifies a unique moment, even though the original date value did. Formatting a value is therefore not necessarily reversible.

Commonly used format codes

The sample outputs below assume the date Wednesday, March 4, 2026, at 14:05:09, formatted in the default C or POSIX locale. That locale uses English names. Under other locales, the name-based codes return different text.

Code | Meaning | Example output
%Y | Four-digit year | 2026
%y | Two-digit year | 26
%m | Month, zero-padded | 03
%d | Day of month, zero-padded | 04
%e | Day of month, space-padded (not in every implementation) | " 4"
%H | Hour, 24-hour clock, zero-padded | 14
%I | Hour, 12-hour clock, zero-padded | 02
%M | Minute | 05
%S | Second | 09
%p | AM/PM marker (locale-dependent) | PM
%a | Abbreviated weekday name (locale-dependent) | Wed
%A | Full weekday name (locale-dependent) | Wednesday
%b | Abbreviated month name (locale-dependent) | Mar
%B | Full month name (locale-dependent) | March
%j | Day of year, zero-padded | 063
%u | ISO weekday, Monday = 1 | 3
%V | ISO 8601 week number | 10
%G | ISO 8601 week-based year | 2026
%z | UTC offset as +HHMM or -HHMM | depends on the value's time zone
%Z | Time zone name or abbreviation | depends on platform and value
%c | Locale's preferred date and time | varies by locale
%x | Locale's preferred date | varies by locale
%X | Locale's preferred time | varies by locale
%% | A literal percent sign | %

Some popular extensions are platform-specific. In Python, %f inserts microseconds, but that code is a Python addition and is not part of C strftime. Removing leading zeros with %-d works with the GNU C library and on BSD-derived systems, while Windows uses %#d. Before relying on any such code, check the documentation for the runtime you will actually deploy on. Python's own documentation notes that the full set of supported codes depends on the platform's C library.

Example patterns

from datetime import datetime

d = datetime(2026, 3, 4, 14, 5, 9)

d.strftime("%Y-%m-%d %H:%M:%S")   # '2026-03-04 14:05:09'
d.strftime("%A, %B %d, %Y")       # 'Wednesday, March 04, 2026'  (C/English locale)
d.strftime("%m/%d/%y %I:%M %p")   # '03/04/26 02:05 PM'          (C/English locale)
d.strftime("%G-W%V-%u")           # '2026-W10-3'
d.strftime("%Y%m%d_%H%M%S")       # '20260304_140509'

The second example shows a common surprise. %d always zero-pads, so the output reads "March 04" instead of "March 4." Removing the zero requires one of the platform-specific flags mentioned above, or post-processing the string.

strftime or a date-only format?

People searching for "strftime or date" may mean one of two things. The first is choosing between strftime and the Unix date command. On most Unix-like systems, the date command accepts the same style of percent codes, as in date +%Y-%m-%d, so the two usually behave alike. Exact support still depends on whether the system uses the GNU or BSD version of the command. The second is choosing between a full timestamp and a date-only string. Include the time and offset if the value marks a specific moment. Use a date-only format such as %Y-%m-%d if the value is a calendar date, like a birthday or due date, where time zones would add confusion rather than precision.

Will the result stay readable and usable after copying or sharing?

Because strftime output is plain text, it copies and pastes like any other text. No hidden styling or special characters are added unless you put them in the pattern. The practical risks come from how others interpret the string, not from how it is stored.

  • Ambiguous order: 03/04/26 means March 4 to most US readers and 3 April to readers in many other regions. A four-digit year with month names, or the ISO layout, removes the ambiguity.
  • Sorting: strings built as %Y-%m-%d, optionally followed by %H:%M:%S, sort chronologically as plain text. Month-first or day-first layouts do not.
  • Parsing back: a program can read a string with strptime and the same pattern, but only if the pattern kept enough information. Two-digit years, 12-hour times without %p, and missing offsets all create ambiguity.
  • Locale drift: name-based and locale-preferred codes may produce a different language or layout on another machine, which can break code that expects fixed text.
  • Time zones: %Z output is not standardized across platforms and abbreviations can be ambiguous. For machine-readable output, %z is the more dependable choice.

Accessibility depends on the format chosen. Spelled-out formats such as "Wednesday, March 4, 2026" are generally easier for people to read unambiguously. How a particular screen reader announces compact numeric dates such as 03/04/26 varies by product and settings. No product-specific test evidence was available for this review, so verify with the assistive technology your audience uses rather than assuming one behavior.

On web pages, a common pattern pairs a human-friendly strftime label with a machine-readable value. The HTML time element supports this through its datetime attribute, which holds an ISO 8601 string while the visible text can use any readable format. Search engines and other tools can then read the precise value regardless of the display format. How any specific search engine uses that attribute is not covered by the evidence available here.

Gaps in this review

This overview relies on documented, general behavior of strftime-style formatting. No benchmark data, platform test results, or assistive-technology test results were supplied. Before depending on platform-specific codes, locale output, or screen reader behavior, consult the reference for the language or library you use and test in the actual target environment.