必ずしもパース出来ない文字列をパースするとき
require 'date'
def safe_datetime_parse(string)
DateTime.parse(string)
rescue ArgumentError
nil
end
a = "2015-03-10T22:55:00+00:00"
p DateTime.parse(a)
# => #<DateTime: 2015-03-10T22:55:00+00:00 ((2457092j,82500s,0n),+0s,2299161j)>
a = "2015-0310T22:55:00+00:00"
p safe_datetime_parse(a)
p datetime = DateTime.parse(a) rescue nil
# => nil
# => nil