Search code examples
rubydateformatstrftime

Ruby strftime: Month without leading zero?


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


Solution

  • 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