Search code examples
rubyruby-on-rails-3datestrptime

Ruby Date.strptime difficulties when parsing 13/Nov/11


When parsing a Jira Custom Field containing a date (e.g. 13/Nov/11) I started with this:

elsif custom.customfieldId == "customfield_10282"
@agenda_item.planned_release_date      =  custom.values.to_s

But the database stores it as 11/13/0011. So I got clever and used this:

elsif custom.customfieldId == "customfield_10282"
@agenda_item.planned_release_date      =  Date.strptime(custom.values, "%d/%m/%Y")

And now I get:

private method sub!' called for ["15/Nov/11"]:Jira4R::V2::ArrayOf_xsd_string C:/Ruby187/lib/ruby/1.8/date/format.rb:429:in_strptime_i' C:/Ruby187/lib/ruby/1.8/date/format.rb:401:in scan' C:/Ruby187/lib/ruby/1.8/date/format.rb:401:in_strptime_i' C:/Ruby187/lib/ruby/1.8/date/format.rb:601:in `_strptime' [truncated the rest of the stack]

What am I missing?


Solution

  • This works:

    p Date.strptime('13/Nov/11', "%d/%b/%y")
    

    Your problems:

    • `%Y is the year with 4 digits (2011). Use %y
    • %m is the month in digits (11). Use %b instead (Nov)