Parsing european date format in Ruby/Rails
So you have date in european format DD.MM.YYYY. that you want to parse in ruby? Nothing easier, change parsedate method in ParseDate stdlib.
require ('parsedate')
module ParseDate
class << self
alias_method :old_parsedate, :parsedate unless
method_defined?(:old_parsedate)
end
def self.parsedate(str)
match = /(\d{1,2})\.(\d{1,2})\.(\d{2,4})\.?/.match(str)
return ParseDate.old_parsedate(str) unless match
[match[3].to_i, match[2].to_i, match[1].to_i,
nil, nil, nil, nil, nil]
end
end
On Rails, you can include it in environment.rb file with require ('parsedate_patches.rb').
I am Bojan Mihelac and this blog is dedicated to share code, thoughts, tools and advices I came up with while working in
6 comments
1. pre-filling an input form (from a database entry or some default),
2. parsing the input form from a POST action
3. (optionally) presenting it in regular output
All one has to do is mention the respective attributes in the model, like dt_datum_parse :date_field1, :date_field2, ...
dt_datum_view :date_field1, :date_field2, ...
It provides the "german" date (like "28.02.2007") and works with postgresQL - which is what I need. (It does currently not support "datetime" fields and such.) You're welcome to try it out, optimize, enhance it... http://80.237.237.126/download/rails-plugins/dt_datum-1.0.tar.gz PMc
“I find much easy way: d = Date.strptime(‘07.02.2008’, ‘%d.%m.%Y’) “
clithridiate diversiform wricht misstater teataster nenuphar photogrammetric protropical Kihei Akahi Rental Unit D607 http://www.wise-info.co.uk
Speak your mind: