解析13 / Nov / 11时Ruby Date.strptime遇到困难

时间:2011-11-11 20:55:13

标签: ruby ruby-on-rails-3 date strptime

在解析包含日期的Jira自定义字段时(例如13 / Nov / 11),我开始这样做:

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

但数据库将其存储为11/13/0011。所以我很聪明并且使用了这个:

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

现在我明白了:

私人方法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:在scan' C:/Ruby187/lib/ruby/1.8/date/format.rb:401:in _ strptime_i' C:/Ruby187/lib/ruby/1.8/date/format.rb:601:在`_strptime' [截断堆栈的其余部分]

我错过了什么?

2 个答案:

答案 0 :(得分:2)

如果我正确理解您的问题,请尝试使用此功能:

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

您缩写了月份名称,因此您应该使用b格式指令。除此之外,因为您的年份仅包含最后两个数字,请使用y指令。

答案 1 :(得分:2)

这有效:

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

你的问题:

  • `%Y是4位数的年份(2011)。使用%y
  • %m是数字(11)的月份。请改用%bNov