In a Ruby 1.9 program, I want to format the current time like Thu 1:51 PM
. What format code should I use for the hour of the day (1
in this example)?
Time.now.strftime '%a %I:%M %p' #=> "Thu 01:51 PM"
Time.now.strftime '%a %l:%M %p' #=> "Thu 1:51 PM"
%I
has a leading zero (01
). %l
has a leading space ( 1
). I don’t see any other format code for the hour in the strftime documentation. I can’t use .lstrip
because the space is in the middle of the string. I could use .gsub(/ +/, " ")
, but I’m wondering if there’s a less hacky, simpler way.