strftime Format Reference

All strftime format codes across C, Python, Ruby, PHP, shell with examples.

Reference for strftime format codes: each %-directive's meaning and a live example rendered from a sample date, with notes on POSIX standard codes versus GNU and language-specific extensions.

What is the difference between %Y and %y in strftime?

%Y is the full four-digit year (2026), while %y is the last two digits zero-padded (26). Use %Y to avoid century ambiguity; %y is mainly for compact legacy formats.

strftime format code reference

strftime turns a date/time into a string using %-prefixed format codes. The same codes work across C, Python, Ruby and POSIX shell date, with some platform extensions. This reference lists each directive, what it produces, and a live example rendered from a fixed sample timestamp so you can see the exact output.

How it works

A format string is plain text with embedded directives. strftime("%Y-%m-%d", t) substitutes each % code with the corresponding component of the time t and leaves other characters untouched. So "%H:%M" on 14:30 yields 14:30.

Codes fall into groups:

  • Date%Y (year), %m (month), %d (day), %A/%a (weekday name), %B/%b (month name), %j (day of year).
  • Time%H/%I (24h/12h hour), %M (minute), %S (second), %p (AM/PM).
  • Zone%Z (zone name), %z (UTC offset).
  • Composite%c, %x, %X (locale date/time), %F (%Y-%m-%d), %T (%H:%M:%S).

Portability matters: the codes above are POSIX standard, but GNU extensions like %-d (strip padding), %s (Unix epoch seconds) and %N (nanoseconds) are not universal. PHP’s strftime is deprecated in favour of date(), which uses an unrelated letter scheme entirely.

Tips and example

The sample instant rendered below is Thursday, 11 June 2026, 14:30:05 UTC:

%Y-%m-%d        2026-06-11
%A, %d %B %Y    Thursday, 11 June 2026
%I:%M %p        02:30 PM
%H:%M:%S %Z     14:30:05 UTC
%j (day of yr)  162

A reliable, locale-independent timestamp is "%Y-%m-%dT%H:%M:%S%z", which is essentially ISO 8601. Avoid locale-dependent composites like %c in logs or filenames because their output varies by system. Filter the table below to confirm any code before using it in production.