ScottJShea
ScottJShea

Reputation: 7111

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?

Upvotes: 0

Views: 1085

Answers (2)

knut
knut

Reputation: 27875

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)

Upvotes: 2

WarHog
WarHog

Reputation: 8710

If I properly understand your issue, try to use this:

Date.strptime('13/Nov/11', "%d/%b/%y")  # => 2011-11-13

You have abbreviated month name and there for you should use b format directive. Beside this because your year contains only two last numbers, use y directive.

Upvotes: 2

Related Questions