Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No padding for hours in strftime

Tags:

ruby

strftime

When using strftime @tzformat = "%F,%l:00 %p":

I want exactly one space between the comma and the hour. But %l gives no space for 10, 11 and 12 whereas if I put “ %l” I get two spaces for 0-9 (one from the padding and another from the space I add).

Month has no-padding option. I don’t see the same for hour.

What am I missing?

like image 597
Martin Cleaver Avatar asked Jan 29 '15 23:01

Martin Cleaver


1 Answers

The - modifier removes padding. If you use %-l instead of %l it will not put a space at all, and you can manually add a space.

Time.now.strftime @tzformat = "%F, %-l:00 %p"            #=> "2015-01-29, 8:00 PM"
(Time.now + 3600*2).strftime @tzformat = "%F, %-l:00 %p" #=> "2015-01-29, 10:00 PM"
like image 128
Alexis Andersen Avatar answered Nov 14 '22 06:11

Alexis Andersen