1

I have a birthday column in my csv that should allow user to input multiple date formats and then the file be imported via web page. At the moment the column is only set to one type of format; :birthday => (Date.strptime(row[2], "%mm/%dd/%YY") rescue nil),. Is it possible to have multiple date formats? Thanks!

4

2 回答 2

2

请尝试Date.parse

解析给定的日期和时间表示,并创建一个日期对象。

如果可选的第二个参数为真,并且检测到的年份在“00”到“99”的范围内,则将年份视为 2 位数字形式并使其完整。

例如:

Date.parse('2001-02-03')          #=> #<Date: 2001-02-03 ...>
Date.parse('20010203')            #=> #<Date: 2001-02-03 ...>
Date.parse('3rd Feb 2001')        #=> #<Date: 2001-02-03 ...>

如果您需要更多控制,您可以随时定义自己的解析方法。

于 2012-10-29T21:24:09.483 回答
0

Rails 的 DateTime 尝试自动检测格式。它将检测以下格式:mm/dd/yydd-mm-yyyyyy-mm-ddyyyy/mm/dd

否则,您必须构建自己的方法,尝试使用文件中存在的格式进行不同的解析。

于 2012-10-29T21:24:55.350 回答