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. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

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.

Quick output table — sample timestamp Thursday, 11 June 2026, 14:30:05 UTC

%Y              2026
%y              26
%m              06
%d              11
%H              14
%I              02
%M              30
%S              05
%p              PM
%A              Thursday
%a              Thu
%B              June
%b              Jun
%j              162      (day of year)
%u              4        (ISO weekday, Mon=1)
%F              2026-06-11
%T              14:30:05
%z              +0000
%Z              UTC

The most commonly needed format strings

ISO 8601 / RFC 3339 (use this in logs and APIs):

"%Y-%m-%dT%H:%M:%S%z"   →   2026-06-11T14:30:05+0000

Human-readable date only:

"%A, %d %B %Y"   →   Thursday, 11 June 2026
"%B %d, %Y"      →   June 11, 2026  (US style)

12-hour time with AM/PM:

"%I:%M %p"   →   02:30 PM

Filename-safe timestamp (no colons or spaces):

"%Y%m%d_%H%M%S"   →   20260611_143005

Portability pitfalls

%-d (no zero-padding) removes leading zeros: %-d on the 5th gives 5 not 05. This is a GNU/Linux extension and does not work on macOS, BSD, or Windows without modification. macOS uses %-d too but the implementation differs in edge cases.

%s (Unix epoch) is not in the POSIX standard but works on most Linux and macOS systems. It gives seconds since 1 January 1970 UTC. Do not rely on it in portable scripts.

Locale sensitivity. %A, %B, %c, %x, and %X all produce locale-specific output. On a French-locale system, %A returns jeudi, not Thursday. Use these for display output, not for parsing or storage.

PHP. PHP’s strftime() function uses the same % codes but is deprecated as of PHP 8.1. The replacement date() function uses a completely different letter scheme with no % prefix — d for zero-padded day instead of %d. Check which function your PHP code uses before consulting this reference.

Python. Python’s datetime.strftime() follows the C standard closely. The strptime() function (string to datetime) uses the same codes for parsing, so a format that works for output also works for input.

Tips

A reliable, locale-independent timestamp for logs and filenames 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.