Removing leading zeros with strftime in Ruby

I had no idea you could do this, so I figured I’d share it. You can remove leading zeros when formatting datetimes with strftime() simply by putting a dash / subtraction sign in front of the format specifier. For example:

Date.parse("June 7th 2012").strftime("%A %B %-d %Y")

produces:

"Thursday June 7 2012"

You can do the same thing with leading zeros in time format specifiers as well. It all depends on your version of strftime you have installed though, so I would run some code and see if your current version is capable of it.

Blog comments powered by Disqus