95

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

Shpigford
  • 24,748
  • 58
  • 163
  • 252

4 Answers4

150

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
AndyG
  • 39,700
  • 8
  • 109
  • 143
draegtun
  • 22,441
  • 5
  • 48
  • 71
  • Seems like the minus works just fine with Ruby 1.8.7, thanks! – Rob Feb 28 '11 at 14:46
  • 2
    @Rob: Yes it will work as long as your system's `strftime` supports it (for eg. it doesn't work here on 1.8.7 running on top of Mac OSX Snow Leopard). – draegtun Feb 28 '11 at 18:20
  • Ah yes, I should have clarified that it was 1.8.7 on ubuntu linux – Rob Mar 02 '11 at 13:58
  • Worked for me. 1.9.2p180 on Ubuntu. – B Seven Jul 03 '11 at 00:17
  • Works for me with 1.9.3 on FreeBSD 9 – user569825 Jun 12 '12 at 23:25
  • Works for me with 1.9.3 on OSX Lion – brad Aug 06 '12 at 11:52
  • Doesn't work with REE 1.8.7 on MacOS 10.6.8, but works with REE 1.8.7 on MacOS 10.8.2 – JNN May 03 '13 at 20:36
  • @draegtun: Could you please tell me how to check what `strftime` I have on my system? I have 2 Macbooks running 10.6.8 and 10.8.2 with almost the same RoR environments (REE 1.8.7-2012.02, RVM 1.19.5, Rubygem 1.8.23 and 1.8.15 corresponding) but `strftime('%-m_%-d_%Y')` doesn't work on 10.6.8. Thanks. – JNN May 03 '13 at 20:46
  • Try `man strftime` and look for... `%-* GNU libc extension. Do not do any padding when performing numerical outputs.` (near bottom of manpage here). – draegtun Sep 22 '13 at 09:38
  • This question is for Ruby, but I've noticed that it works in C as well. – hagope Dec 07 '14 at 16:44
42

Here's the formatting list I go off of. This is from the docs for 2.1.3. According to this you would want %-m:

Date (Year, Month, Day):
  %Y - Year with century (can be negative, 4 digits at least)
          -0001, 0000, 1995, 2009, 14292, etc.
  %C - year / 100 (rounded down such as 20 in 2009)
  %y - year % 100 (00..99)

  %m - Month of the year, zero-padded (01..12)
          %_m  blank-padded ( 1..12)
          %-m  no-padded (1..12)
  %B - The full month name (``January'')
          %^B  uppercased (``JANUARY'')
  %b - The abbreviated month name (``Jan'')
          %^b  uppercased (``JAN'')
  %h - Equivalent to %b

  %d - Day of the month, zero-padded (01..31)
          %-d  no-padded (1..31)
  %e - Day of the month, blank-padded ( 1..31)

  %j - Day of the year (001..366)

Time (Hour, Minute, Second, Subsecond):
  %H - Hour of the day, 24-hour clock, zero-padded (00..23)
  %k - Hour of the day, 24-hour clock, blank-padded ( 0..23)
  %I - Hour of the day, 12-hour clock, zero-padded (01..12)
  %l - Hour of the day, 12-hour clock, blank-padded ( 1..12)
  %P - Meridian indicator, lowercase (``am'' or ``pm'')
  %p - Meridian indicator, uppercase (``AM'' or ``PM'')

  %M - Minute of the hour (00..59)

  %S - Second of the minute (00..60)

  %L - Millisecond of the second (000..999)
       The digits under millisecond are truncated to not produce 1000.
  %N - Fractional seconds digits, default is 9 digits (nanosecond)
          %3N  millisecond (3 digits)
          %6N  microsecond (6 digits)
          %9N  nanosecond (9 digits)
          %12N picosecond (12 digits)
          %15N femtosecond (15 digits)
          %18N attosecond (18 digits)
          %21N zeptosecond (21 digits)
          %24N yoctosecond (24 digits)
       The digits under the specified length are truncated to avoid
       carry up.

Time zone:
  %z - Time zone as hour and minute offset from UTC (e.g. +0900)
          %:z - hour and minute offset from UTC with a colon (e.g. +09:00)
          %::z - hour, minute and second offset from UTC (e.g. +09:00:00)
  %Z - Abbreviated time zone name or similar information.

Weekday:
  %A - The full weekday name (``Sunday'')
          %^A  uppercased (``SUNDAY'')
  %a - The abbreviated name (``Sun'')
          %^a  uppercased (``SUN'')
  %u - Day of the week (Monday is 1, 1..7)
  %w - Day of the week (Sunday is 0, 0..6)

ISO 8601 week-based year and week number:
The first week of YYYY starts with a Monday and includes YYYY-01-04.
The days in the year before the first week are in the last week of
the previous year.
  %G - The week-based year
  %g - The last 2 digits of the week-based year (00..99)
  %V - Week number of the week-based year (01..53)

Week number:
The first week of YYYY that starts with a Sunday or Monday (according to %U
or %W). The days in the year before the first week are in week 0.
  %U - Week number of the year. The week starts with Sunday. (00..53)
  %W - Week number of the year. The week starts with Monday. (00..53)

Seconds since the Epoch:
  %s - Number of seconds since 1970-01-01 00:00:00 UTC.

Literal string:
  %n - Newline character (\n)
  %t - Tab character (\t)
  %% - Literal ``%'' character

Combination:
  %c - date and time (%a %b %e %T %Y)
  %D - Date (%m/%d/%y)
  %F - The ISO 8601 date format (%Y-%m-%d)
  %v - VMS date (%e-%^b-%4Y)
  %x - Same as %D
  %X - Same as %T
  %r - 12-hour time (%I:%M:%S %p)
  %R - 24-hour time (%H:%M)
  %T - 24-hour time (%H:%M:%S)

Updated to latest 2.1.3 docs on 10/24/14

Rob Cameron
  • 9,674
  • 7
  • 39
  • 42
  • 7
    I'm looking for how to get the month without the leading zero...not the day. – Shpigford Sep 30 '10 at 19:09
  • 2
    Why is everyone voting this post DOWN? I would have thought having this list of strftime helpers would be useful! – Rob Cameron May 05 '11 at 22:41
  • 6
    I imagine that people are voting it down because it doesn't answer the question. By itself, the list is quite useful, but none of these options prevent the leading "0" for the month as the OP requested. – Benry Jun 29 '11 at 02:40
  • 1
    +1 Despite this question not answering the original question, it is useful to other people who google for something similar, but not exactly the OP. – David Oneill Jul 15 '11 at 15:34
  • 1
    %e provides a leading space (i.e. " 1/01/2012") which can be undesirable and is not necessarily what the OP wanted. – brad Aug 06 '12 at 11:53
10

Docs show a number of different options for configuring number format. Adding to the %-d format, you can also use these flags in place of "-":

Flags:
  -  don't pad a numerical output.
  _  use spaces for padding.
  0  use zeros for padding.
  ^  upcase the result string.
  #  change case.
  :  use colons for %z.
typeoneerror
  • 55,990
  • 32
  • 132
  • 223
7

I had a similar problem and fixed it by converting strftime("%m") into an integer.

For example:

strftime("%m")+0 give the current month as integer 'without leading zero'

Simple, though not elegant.

Verbeia
  • 4,400
  • 2
  • 23
  • 44
cyberw0rm
  • 71
  • 1
  • 1