Ruby strftime tokens.

Ruby's strftime is the C vocabulary made dependable: the padding flags (- for none, _ for blanks, 0 for zeros) and the casing flags (^ upcase, # swapcase) are documented as part of the language, so %-d and %-m work everywhere Ruby runs — no Windows caveat as in Python. The same table serves Date, DateTime and Time. Week-based tokens (%G, %V) follow ISO 8601. There are no tokens for quarters or eras.

TokenMeansExample
%YFour-digit year

Zero-padded, includes the century.

2006
%yTwo-digit year06
%-mMonth number

The minus is a padding flag on %m — documented, portable Ruby.

1
%mMonth number, padded01
%bMonth name, abbreviated

%h is a documented synonym.

Jan
%BMonth name, full

%^B upcases it via the casing flag.

January
%-dDay of month

The minus is a padding flag on %d; %e gives the blank-padded form instead.

2
%dDay of month, padded02
%aWeekday, abbreviatedMon
%AWeekday, fullMonday
%jDay of year

Zero-padded to three digits, range 1..366.

2
%VWeek of year

ISO-8601 week, range 1..53. %U (Sunday-first) and %W (Monday-first) count 0..53 and disagree around New Year.

1
%GISO week-based year

Pairs with %V; mixing %Y with %V is the classic New-Year bug. %g is its two-digit form.

2006
%HHour, 24-hour clock, padded

%k is the blank-padded form; %-H strips padding entirely.

15
%IHour, 12-hour clock, padded

%l is the blank-padded form.

03
%pAM/PM marker

Uppercase AM/PM; %P gives lowercase am/pm — the case of the letter is inverted from what you might expect.

PM
%MMinute, padded04
%SSecond, padded05
%LFractional seconds

Milliseconds, exactly three digits. %N is nanoseconds (nine digits) and takes a width: %6N for microseconds.

000
%zUTC offset, basic-0700
%:zUTC offset, extended

%::z adds seconds to the offset.

-07:00
%ZTime-zone name, short

Platform-dependent name, e.g. Central Daylight Time.

MST
%sUnix timestamp1136239445

Examples render the reference instant, Mon, Jan 2 2006, 3:04:05 PM MST.

Literal text

A literal percent sign is written %%; %n and %t emit a newline and a tab. All other text passes through unchanged.

Verified against Ruby docs — Formats for Dates and Times.

Translate Ruby strftime