European Date Format (newbie!)

Rails 2.0

Hi there,

There are a few topics on here that relate to my problem but none are
helping as i’m new to this and there are lots of confusing posts on
google.

I need to chage the way dates are input and displayed in my app. This
is for a european app so the dates need to work as DD/MM/YYYY not the
America way.

Now I don’t mind keeping the datbase in the orginal Date format as I
won’t be accessing it directly however I guess I need to wrtite
something in my model to change the way the dates are done?

Anybody able to help?

Many thanks.

Nick

On 21 Oct 2008, at 22:21, Nicktabs wrote:

something in my model to change the way the dates are done?
Showing them in European date format is easy, almost every
localization plugin out there provides it in some way and you can do
it yourself quite easily too. Input is a totally different matter, as
far as I know there is no transparent way to make Rails process
European date and number formats from input fields. I’ve implemented a
few hacky solutions in the past, like using method_missing or
generating a number of new methods to convert one format to the other,
but in the end it doesn’t feel right at all.

There have been some discussions in the Rails i8n group about it, but
those also seem to have died a silent death and localization in the
next version is very basic (but easier to plugin on, although I still
have serious doubts about date and number formats, since those would
require quite some changes in the fundations of the framework).

I hope someone of the i8n people will chime in on this post and be
able to tell you what to expect, I might be wrong in what I said (so
hung up in a project I haven’t been able to follow the discussion).

Best regards

Peter De Berdt

I need to chage the way dates are input and displayed in my app. This
is for a european app so the dates need to work as DD/MM/YYYY not the
America way.

Now I don’t mind keeping the datbase in the orginal Date format as I
won’t be accessing it directly however I guess I need to wrtite
something in my model to change the way the dates are done?

I use the DATE format in the database. I convert the format on input
and output.

Input from a form:
def convert_european_date(date)

# Authorize ISO date
return date unless (/^\d{4}-\d{2}-\d{2}$/).match(date).nil?

# Convert european date
match = /(\d{1,2}).(\d{1,2}).(\d{2,4})/.match(date)
begin
  Date.new(match[3].to_i, match[2].to_i, match[1].to_i)
rescue
  return false
end

end

Display:
<%= due_date.to_s(:european) %>

In initializers/date_formats.rb:
ActiveSupport::CoreExtensions::date::Conversions::DATE_FORMATS.merge!
(:european => ‘%d/%m/%Y’)
ActiveSupport::CoreExtensions::date::Conversions::DATE_FORMATS.merge!
(:american => ‘%m/%d/%Y’)

Christian

On Tue, Oct 21, 2008 at 4:21 PM, Nicktabs [email protected] wrote:

I need to chage the way dates are input and displayed in my app. This
is for a european app so the dates need to work as DD/MM/YYYY not the
America way.

Not a complete solution, but building blocks:

http://ruby-doc.org/core/classes/Date.html#M000655
http://ruby-doc.org/core/classes/Date.html#M000620
http://www.ruby-doc.org/core/classes/Time.html#M000297

puts Date.strptime(“22/10/2008”, “%d/%m/%Y”).strftime(“%d/%m/%Y”)