How to find a time format for Ruby's strftime

Joe Ferris

Have you written a web application lately? If so, I bet you’ve been using good old strftime. I use this on every application I write, but the only format token I ever remember is “%m is for month.” Or is it minute?

You could pop open ri, man, cheat, or For a Good Strftime, but there’s a fun wrinkle: strftime behaves differently in Ruby 1.8 and 1.9, and the 1.8 version even varies from system to system. For a weird situation like this, you can’t trust documentation; there’s only one thing that doesn’t lie: code.

You can fire this little script up on your local machine or your server to get a table that looks like the following:

curl -L http://git.io/0QdLVw > time_formats.rb
ruby time_formats.rb

a        Tue                       Thu                       Sat
b        Nov                       Nov                       Dec
c        Tue Nov  1 00:00:00 2011  Thu Nov 17 00:14:27 2011  Sat Dec 31 23:59:59 2011
d        01                        17                        31
e         1                        17                        31
f        %f                        %f                        %f
g        11                        11                        11
h        Nov                       Nov                       Dec
i        %i                        %i                        %i
j        305                       321                       365
k         0                         0                        23
l        12                        12                        11
m        11                        11                        12
n
o        %o                        %o                        %o
p        AM                        AM                        PM
q        %q                        %q                        %q
r        12:00:00 AM               12:14:27 AM               11:59:59 PM
s        1320120000                1321506867                1325393999
t
u        2                         4                         6
v         1-NOV-2011               17-NOV-2011               31-DEC-2011
w        2                         4                         6
x        11/01/11                  11/17/11                  12/31/11
y        11                        11                        11
z        -0400                     -0500                     -0500
A        Tuesday                   Thursday                  Saturday
B        November                  November                  December
C        20                        20                        20
D        11/01/11                  11/17/11                  12/31/11
E        %E                        %E                        %E
F        2011-11-01                2011-11-17                2011-12-31
G        2011                      2011                      2011
H        00                        00                        23
I        12                        12                        11
J        %J                        %J                        %J
K        %K                        %K                        %K
L        000                       599                       999
M        00                        14                        59
N        000000000                 599545000                 999999998
O        %O                        %O                        %O
P        am                        am                        pm
Q        %Q                        %Q                        %Q
R        00:00                     00:14                     23:59
S        00                        27                        59
T        00:00:00                  00:14:27                  23:59:59
U        44                        46                        52
V        44                        46                        52
W        44                        46                        52
X        00:00:00                  00:14:27                  23:59:59
Y        2011                      2011                      2011
Z        EDT                       EST                       EST

It prints a few different dates so you can tell which is the date and which is the hour, as well as whether or not you’re going to get leading spaces or zeros, or 12-hour vs 24-hour output.