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.
| Token | Means | Example |
|---|---|---|
| %Y | Four-digit year Zero-padded, includes the century. | 2006 |
| %y | Two-digit year | 06 |
| %-m | Month number The minus is a padding flag on %m — documented, portable Ruby. | 1 |
| %m | Month number, padded | 01 |
| %b | Month name, abbreviated %h is a documented synonym. | Jan |
| %B | Month name, full %^B upcases it via the casing flag. | January |
| %-d | Day of month The minus is a padding flag on %d; %e gives the blank-padded form instead. | 2 |
| %d | Day of month, padded | 02 |
| %a | Weekday, abbreviated | Mon |
| %A | Weekday, full | Monday |
| %j | Day of year Zero-padded to three digits, range 1..366. | 2 |
| %V | Week of year ISO-8601 week, range 1..53. %U (Sunday-first) and %W (Monday-first) count 0..53 and disagree around New Year. | 1 |
| %G | ISO week-based year Pairs with %V; mixing %Y with %V is the classic New-Year bug. %g is its two-digit form. | 2006 |
| %H | Hour, 24-hour clock, padded %k is the blank-padded form; %-H strips padding entirely. | 15 |
| %I | Hour, 12-hour clock, padded %l is the blank-padded form. | 03 |
| %p | AM/PM marker Uppercase AM/PM; %P gives lowercase am/pm — the case of the letter is inverted from what you might expect. | PM |
| %M | Minute, padded | 04 |
| %S | Second, padded | 05 |
| %L | Fractional seconds Milliseconds, exactly three digits. %N is nanoseconds (nine digits) and takes a width: %6N for microseconds. | 000 |
| %z | UTC offset, basic | -0700 |
| %:z | UTC offset, extended %::z adds seconds to the offset. | -07:00 |
| %Z | Time-zone name, short Platform-dependent name, e.g. Central Daylight Time. | MST |
| %s | Unix timestamp | 1136239445 |
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.