Remove padding from values in strftime
By default, some numbers in strftime are padded, either with 0 or ' '
.
For example:
best_moment_ever = DateTime.new(1996, 2, 15, 19, 21, 0, '-05:00')
=> Thu, 15 Feb 1996 19:21:00 -0500
best_moment_ever.strftime("%m/%e/%Y at %l:%M%P")
=> "02/15/1996 at 7:21pm"
As we can see, there is a big gap between at
and 7:21pm
. This is because the hour is being padded with empty string. Sometimes this is fine, but if you ever wanted to remove any padding, just add a -
flag to the directive:
best_moment_ever.strftime("%-m/%e/%Y at %-l:%M%P")
=> "2/15/1996 at 7:21pm"
Notice how we also removed other built in padding, like the 0 in the month
There's a few other ways you can manipulate the results. Learn more here!
Tweet