Ruby strftime: Month without leading zero?

Shpigford picture Shpigford · Sep 30, 2010 · Viewed 41k times · Source

Does Ruby's strftime have a format for the month without a leading zero?

I found %e for getting the day without the leading zero, but not having any luck with the month.

Ultimately wanting a date formatted like: 9/1/2010

Answer

draegtun picture draegtun · Sep 30, 2010

Some versions of strftime do allow prefixing with minus to format out leading zeros, for eg:

strftime "%-d/%-m/%y"

However this will depend on strftime on your system. So for consistency I would do something like this instead:

dt = Time.local(2010, 'Sep', 1)
printf "%d/%d/%d", dt.day, dt.month, dt.year