Topic: text_field datetime format

Hi,

first of all introduce myself because this is the first time I write here. I'm new to Rails and after a period of learning I'm now develping an application.

I'm from Spain, and as you know here we have different date format. I've searched a solution but haven't found a response.

I explain my case.
I've added the following to environment.rb

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(:default => '%d/%m/%Y %H:%M:%S')

This runs fine for list and show pages... but when I go to edit, Rails doesn't respect the format.
To solve this, I've added:
<%= text_field 'banner', 'created_at', :value => @banner.created_at.to_s %>

And this runs fine... in apparience, because is just the viewing format.

When the value is stored to the database (mysql with YYYY-MM-DD HH:MM:SS datetime format) values for month and day are inversed.

e.g. if I write 11/12/2006 => nov 11 2006 in the database I get 2006-12-11 => dec 12 2006. More over, if I write 22/11/2006 the datetime inserted in the database is 0000-00-00 00:00:00, and when I go back to the list I get a nil exception sad

How can I solve that?

I have a javascript free calendar associated to this field, so the datetime_select solution won't work for me (moreover I can not specify the selects orders as in date_select the method :S).

Thanks in advance

Re: text_field datetime format

What I would do is create a getter and setter method for the formatted time string. Like this:

# in banner.rb
def formatted_created_at
  created_at.strftime('%d/%m/%Y %H:%M:%S')
end

def formatted_created_at=(time_str)
  self.created_at = Time.parse(time_str)
end

# in view
<%= text_field 'banner', 'formatted_created_at' %>


Now this new getter/setter method will be used.

This in itself won't solve your problem because I'm using the Time.parse() method which doesn't seem to support the european DD/MM/YY format. However, at least now you can customize the parsing. You can either parse it manually or find some other helper. There's a hack for ParseDate which might do what you want.

Edit: This might work for parsing:

self.created_at = DateTime.strptime(time_str, '%d/%m/%Y %H:%M:%S').to_time

Last edited by ryanb (2006-11-23 18:08:30)

Railscasts - Free Ruby on Rails Screencasts

Re: text_field datetime format

I've done what you told me but i get a new error:

 ArgumentError in Admin/bannerController#update

argument out of range

RAILS_ROOT: ./script/../config/..
Application Trace | Framework Trace | Full Trace

E:/ruby/lib/ruby/1.8/time.rb:184:in `local'
E:/ruby/lib/ruby/1.8/time.rb:184:in `make_time'
E:/ruby/lib/ruby/1.8/time.rb:243:in `parse'
#{RAILS_ROOT}/app/models/banner.rb:13:in `formatted_created_at='
-e:4:in `load'
-e:4

E:/ruby/lib/ruby/1.8/time.rb:184:in `local'
E:/ruby/lib/ruby/1.8/time.rb:184:in `make_time'
E:/ruby/lib/ruby/1.8/time.rb:243:in `parse'
E:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1510:in `send'
E:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1510:in `attributes='
E:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1509:in `each'
E:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1509:in `attributes='
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/scaffolding.rb:145:in `update'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:941:in `send'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:941:in `perform_action_without_filters'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:368:in `perform_action_without_benchmark'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
E:/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/rescue.rb:82:in `perform_action'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:in `send'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:in `process_without_filters'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:377:in `process_without_session_management_support'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/session_management.rb:117:in `process'
E:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/dispatcher.rb:38:in `dispatch'
E:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:115:in `handle_dispatch'
E:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:81:in `service'
E:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
E:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
E:/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
E:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
E:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
E:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
E:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
E:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
E:/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
E:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
E:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:67:in `dispatch'
E:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/servers/webrick.rb:59
E:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
E:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
E:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in `require'
E:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/server.rb:30
E:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
E:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
E:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in `require'
script/server:3

E:/ruby/lib/ruby/1.8/time.rb:184:in `local'
E:/ruby/lib/ruby/1.8/time.rb:184:in `make_time'
E:/ruby/lib/ruby/1.8/time.rb:243:in `parse'
#{RAILS_ROOT}/app/models/banner.rb:13:in `formatted_created_at='
E:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1510:in `send'
E:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1510:in `attributes='
E:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1509:in `each'
E:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1509:in `attributes='
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/scaffolding.rb:145:in `update'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:941:in `send'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:941:in `perform_action_without_filters'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:368:in `perform_action_without_benchmark'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
E:/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/rescue.rb:82:in `perform_action'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:in `send'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:in `process_without_filters'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:377:in `process_without_session_management_support'
E:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/session_management.rb:117:in `process'
E:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/dispatcher.rb:38:in `dispatch'
E:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:115:in `handle_dispatch'
E:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:81:in `service'
E:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
E:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
E:/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
E:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
E:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
E:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
E:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
E:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
E:/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
E:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
E:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:67:in `dispatch'
E:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/servers/webrick.rb:59
E:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
E:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
E:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in `require'
E:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/server.rb:30
E:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
E:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
E:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in `require'
script/server:3
-e:4:in `load'
-e:4

Request

Parameters: {"banner"=>{"publicado"=>"0", "codigo"=>"codigo banner 5", "titulo"=>"banner 5", "url"=>"url banner 5", "precio"=>"500", "formatted_created_at"=>"20/11/2006 22:04:14", "foto"=>"foto banner 5", "sponsor"=>"500"}, "commit"=>"Edit", "id"=>"8"}


This is when I insert a date of this type: 20/11/2006 hh:mm:ss

I think it is because of the 20, it's still understanding it as the month...

More over, this technique means that I have to tune each model of my app for every date...
I'll try tomorrow hack for parseDate. I will inform here smile

Last edited by brusulaf (2006-11-24 15:07:07)

Re: text_field datetime format

Yeah, that error is expected with the Time.parse method. But, did you try the DateTime.strptime method? I don't think you will get that error with that - at least I don't in script/console.

Anyway, once you get this working you can probably make a class method in ActiveRecord::Base which generates this getter/setter method dynamically. I'm thinking the interface would look like this:

class Banner < ActiveRecord::Base
  formatted_time_attr :created_at, :updated_at # etc...
end

You could then use this method on any model to automatically generate the getter/setter methods. I'm not exactly sure how to implement this though. If you need me to I can find out.

There's probably a deep hack which you could do in Ruby/Rails which will make all time parsing switch the month/day, but I have no clue where to start looking.

Railscasts - Free Ruby on Rails Screencasts

Re: text_field datetime format

ryanb wrote:

Yeah, that error is expected with the Time.parse method. But, did you try the DateTime.strptime method? I don't think you will get that error with that - at least I don't in script/console.

With that it works great!! (i hadn't time to test it yesterday)
Thak you very much

ryanb wrote:

Anyway, once you get this working you can probably make a class method in ActiveRecord::Base which generates this getter/setter method dynamically. I'm thinking the interface would look like this:

class Banner < ActiveRecord::Base
  formatted_time_attr :created_at, :updated_at # etc...
end

You could then use this method on any model to automatically generate the getter/setter methods. I'm not exactly sure how to implement this though. If you need me to I can find out.

There's probably a deep hack which you could do in Ruby/Rails which will make all time parsing switch the month/day, but I have no clue where to start looking.

I'm gonna search the way to do this right now.
It's a good start point smile
Thanks again Ryan

Of course, I will inform of my results smile

Last edited by brusulaf (2006-11-25 09:08:31)

Re: text_field datetime format

As a first approximation to the problem, I've made an intermediate class between ActiveRecord::Base called BaseEuropeanDate (in lib/base_european_date.rb)

class BaseEuropeanDate < ActiveRecord::Base
   def formatted_created_at
     created_at.strftime('%d/%m/%Y %H:%M:%S')
   end
   
   def formatted_created_at=(time_str)
     self.created_at = DateTime.strptime(time_str, '%d/%m/%Y %H:%M:%S').to_time
   end
end

of course, I've also changed my banner model definition
class Banner < BaseEuropeanDate

And everything works perfectly


I have to find now how to do it for not being date name independent

Re: text_field datetime format

I think the approach I mentioned above will be better. Using a subclass for something like this can be limiting. Here's how you would create a class method to handle this:

class ActiveRecord::Base
  def self.formatted_time_accessor(*names)
    names.each do |name|
      define_method("formatted_#{name}") do
        self[name].strftime('%d/%m/%Y %H:%M:%S')
      end
      define_method("formatted_#{name}=") do |value|
        self[name] = DateTime.strptime(value, '%d/%m/%Y %H:%M:%S').to_time
      end
    end
  end
end

Place this in a file in the lib directory and require it in environment.rb. Then you can call this method at the top of any model to automatically create a formatted method. You can pass multiple arguments in as well. For example:

class Banner < ActiveRecord::Base
  formatted_time_accessor :created_at, :updated_at
end

This will create the formatted_created_at and formatted_updated_at getter/setter methods.

Railscasts - Free Ruby on Rails Screencasts

Re: text_field datetime format

ryanb wrote:

I think the approach I mentioned above will be better. Using a subclass for something like this can be limiting.

Yes it is, in fact I have to specify

set_table_name "banners"

in all my models, because rails complains that base_european_dates was not a table of my database

ryanb wrote:

Here's how you would create a class method to handle this:

class ActiveRecord::Base
  def self.formatted_time_accessor(*names)
    names.each do |name|
      define_method("formatted_#{name}") do
        self[name].strftime('%d/%m/%Y %H:%M:%S')
      end
      define_method("formatted_#{name}=") do |value|
        self[name] = DateTime.strptime(value, '%d/%m/%Y %H:%M:%S').to_time
      end
    end
  end
end

Place this in a file in the lib directory and require it in environment.rb. Then you can call this method at the top of any model to automatically create a formatted method. You can pass multiple arguments in as well. For example:

class Banner < ActiveRecord::Base
  formatted_time_accessor :created_at, :updated_at
end

This will create the formatted_created_at and formatted_updated_at getter/setter methods.

Yesterday evenning I searched an eval solution for concatenate the names of dates... but with no result sad

Your solution is what I was looking for smile
Thnk you very much Ryan

Just a little correction:

class ActiveRecord::Base
  def self.formatted_time_accessor(*names)
    names.each do |name|
      define_method("formatted_#{name}") do
        self[name].strftime('%d/%m/%Y %H:%M:%S') if self[name]#for new, with of nil date attribute
      end

      define_method("formatted_#{name}=") do |value|
        self[name] = DateTime.strptime(value, '%d/%m/%Y %H:%M:%S').to_time
      end
    end
  end
end


I'd to add a nil comparison, because without it new pages didn't run.
I get
You have a nil object when you didn't expect it!
The error occured while evaluating nil.strftime

But now I'm getting a strange behaviour. I'm losting hours, minutes and seconds. And that happens in both new and edit pages.
When editing, I get the right attribute value in the text field, but after submiting the from... hour, minutes and seconds values are not passed through. Very weird.

I'll investigate that.

Thanks again Ryan, I've learned a lot with this post and your solutions.

Re: text_field datetime format

Hmm, perhaps the hours/minutes/seconds are somehow being lost during the parsing. I would play around with the model in script/console (setting the formatted_... attribute) and see if it is set properly.

The created_at column in the database is of type "datetime", correct? Not just "date"?

Railscasts - Free Ruby on Rails Screencasts

Re: text_field datetime format

ryanb wrote:

Hmm, perhaps the hours/minutes/seconds are somehow being lost during the parsing. I would play around with the model in script/console (setting the formatted_... attribute) and see if it is set properly.

it would be great... thank you for your time

ryanb wrote:

The created_at column in the database is of type "datetime", correct? Not just "date"?

Yes, they are



PD: Excuse my late response... but I've lost my home adsl comnnection...and I'd to search my modem (while they repair the problem)

Last edited by brusulaf (2006-11-29 13:12:43)

Re: text_field datetime format

brusulaf wrote:
ryanb wrote:

Hmm, perhaps the hours/minutes/seconds are somehow being lost during the parsing. I would play around with the model in script/console (setting the formatted_... attribute) and see if it is set properly.

it would be great... thank you for your time

Sorry, I meant that's what I suggest you do. Have you tried calling the formatted_created_at getter/setter methods in script/console? Do the seconds get lost there?

Railscasts - Free Ruby on Rails Screencasts

Re: text_field datetime format

ryanb wrote:

Sorry, I meant that's what I suggest you do. Have you tried calling the formatted_created_at getter/setter methods in script/console? Do the seconds get lost there?

Oh! excuse me... I misundestood you. (it sounded very strange to me smile )

I don't know how to test things on the console.
I'll look for it and try it right now.

Excuse my english ryan sad

Thanks again

Re: text_field datetime format

No problem.

If you go to your rails app directory in the command line and run script/console you will get a little testing environment:

ruby script/console

Here you can run ruby commands and instantly see the results, just like IRB. Here's some commands you can run.

b = Banner.find(:first)
b.created_at # look at the actual created_at Time
b.formatted_created_at # just to see the response
b.formatted_created_at = "20/11/2006 10:27:03"
b.formatted_created_at # see if it updated properly
b.created_at # see if this updated too
b.save
b.reload
b.formatted_created_at # see if it is correctly saving and fetching

Last edited by ryanb (2006-11-29 15:00:44)

Railscasts - Free Ruby on Rails Screencasts

Re: text_field datetime format

I've tested it and it also losts time.

W:\>ruby script/console development
Loading development environment.
>> b= Banner.find(:first)
=> #<Banner:0x37ac800 @attributes={"publicado"=>"0", "codigo"=>"codigo banner 5", "titulo"=>"banner 5", "url"=>"url banner 5", "id"=>"8", "precio"=>"500", "sponsor"=>"500", "foto"=>"foto banner 5", "created_at"=>"2006-11-25 15:16:17"}>
>> b.created_at
=> Sat Nov 25 15:16:17 +0100 2006
>> b.formatted_created_at
=> "25/11/2006 15:16:17"
>> b.formatted_created_at='20/11/2006 10:27:03'
=> "20/11/2006 10:27:03"
>> b.formatted_created_at
=> "20/11/2006 00:00:00"
>> b.created_at
=> Mon Nov 20 00:00:00 +0100 2006
>> b.save
=> true
>> b.reload
=> #<Banner:0x37ac800 @errors=#<ActiveRecord::Errors:0x378f304 @errors={}, @base=#<Banner:0x37ac800 ...>>, @banners_views=nil, @attributes={"publicado"=>"0", "codigo"=>"codigo banner 5", "titulo"=>"banner 5", "url"=>"url banner 5", "id"=>"8", "precio"=>"500", "sponsor"=>"500", "foto"=>"foto banner 5", "created_at"=>"2006-11-20 00:00:00"}, @banners_hits_views=nil, @new_record_before_save=nil>
>> b.formatted_created_at
=> "20/11/2006 00:00:00"
>> exit

Last edited by brusulaf (2006-11-30 14:07:10)

Re: text_field datetime format

So the seconds are getting lost during the conversion. Try this in script/console.

DateTime.strptime('20/11/2006 10:27:03', '%d/%m/%Y %H:%M:%S').to_time

It should return the Time with the seconds - at least it does for me. So this part is working correctly. What I don't see is why the seconds would be getting lost when setting created_at. Are you certain it is of type Time?

b = Banner.find(:first)
b.created_at.class # should return Time

Try setting the current time:

b.created_at = Time.now

Do the seconds get lost that way too?

Railscasts - Free Ruby on Rails Screencasts

Re: text_field datetime format

here is the trace

W:\>ruby script/console development
Loading development environment.
>> DateTime.strptime('20/11/2006 10:27:03', '%d/%m/%Y %H:%M:%S').to_time
=> Mon Nov 20 00:00:00 +0100 2006
>> b = Banner.find(:first)
=> #<Banner:0x37a32b4 @attributes={"publicado"=>"0", "codigo"=>"codigo banner 5", "titulo"=>"banner 5", "url"=>"url banner 5", "id"=>"8", "precio"=>"500", "sponsor"=>"500", "foto"=>"foto banner 5", "created_at"=>"2006-11-20 00:00:00"}>
>> b.created_at.class
=> Time
>> b.created_at=Time.now
=> Fri Dec 01 18:57:35 +0100 2006
>> b.formatted_created_at
=> "01/12/2006 18:57:35"
>> exit

It seems that it doesn't like the format for creating the date... but the one for returning it works perfectly.

Re: text_field datetime format

Ohhh, I was testing it in edge rails. I tested it in 1.1.6 and it strips off the seconds as well:

# in 1.1.6:
>> DateTime.strptime('20/11/2006 10:27:03', '%d/%m/%Y %H:%M:%S').to_time
#=> Mon Nov 20 00:00:00 PST 2006

# in edge:
DateTime.strptime('20/11/2006 10:27:03', '%d/%m/%Y %H:%M:%S').to_time
#=> Mon Nov 20 10:27:03 PST 2006


Hopefully 1.2 will be coming out soon, so this should fix the problem. In the meantime you can try patching it yourself. This apparently works:

class DateTime
  def to_time
    Time.parse(strftime("%Y-%m-%d %H:%M:%S"))
  end
end

You can place this in a file in the lib directory and "require" it in environment.rb

Railscasts - Free Ruby on Rails Screencasts

Re: text_field datetime format

ooh!

So it's solved, but just in the edge version...

ok, I've added the date_time.rb file and require it.

Now the console trace is the following:

W:\>ruby script/console development
Loading development environment.
>> DateTime.strptime('20/11/2006 10:27:03', '%d/%m/%Y %H:%M:%S').to_time
=> Mon Nov 20 10:27:03 +0100 2006

So it works. smile

Thank you very much Ryan, I appreciate very much your help and your patiente.
As I've said before, I've learned so much.

Best regards

Re: text_field datetime format

Hi all,
based on your suggestions, I used your method in order to format and save dates in European format, and put

formatted_date_accessor :begin_date, :end_date

at the beginning of the model, and a class in the lib directory.
But the validation clauses such as:
validates_presence_of :begin_date
validates_presence_of :end_date

stopped working.
I tried also
validates_presence_of :format_begin_date
validates_presence_of :format_end_date

but it does not work either.
Is there some trick to perform validation on these "modified" fields too?

Thanks a lot in advance.

Last edited by axplains (2009-01-29 11:20:48)